def anti_vowel(text):
t=""
for c in text
for i in "ieaouIEAOU":
if c==i:
c=''
else:
c=c
t=t+c
return t
this code is for skipping the vowels and print consonants.
same thing if i rewrite like the code below to print only vowels.why do i get empty result
just forget about indentation.
def anti_vowel(text):
t=""
for c in text:
for i in "ieaouIEAOU":
if c==i:
c=c
else:
c=""
t=t+c
return t
both approaches share the same problem.
which is mostly here:
for i in "ieaouIEAOU":
if c==i:
c=c
else:
c=""
only after this for loop has finished running, we want to append c
to t
(t = t + c
). Given then we determined i
is not in ieaouIEAOU
.
many people new to programming struggle with nested loops and conditions
lets say we have:
anti_vowel("the")
your else
clause will execute 10 times for the first letter (t), then 10 times for the second letter (h) and 9 times for the third letter (e)
we only want to execute else once per letter, after we use for i in "ieaouIEAOU"
to determine i
is not in vowels string.
i tried but i am not getting result.for ex i want to print only vowels from “i love las vegas”.normally that code would return “lv ls vgs” but i want to return only vowels “i oe a ea”.but i am getting result like “ioeaea” or only first vowel i.e “i”.can you post the code to print vowels with spaces
i explained where you went with your loop, please respond to my explanation.
what you could do is when c
equals i
, set c
to an empty string
then after the inner loop, append c
to t
, this will append the consonants to t
, or an empty string when c
would have been a vowel, given you caught this will your inner loop and if condition.
get rid of the else clause in the inner loop, you don’t want to do something when c
doesn’t equal i
.
its important to understand how many times your if and else clause are currently executing, and how this is affecting your program, maybe insert some print statements?