Honestly, this solution is just dreadful. I know its the proposed solution, so my dreadful is not personally towards you 
i can highly recommend to run the code through a visualizer to see the steps:
http://www.pythontutor.com/visualize.html#mode=edit
lets try one together:
anti_vowel("me")
then we get to this line (the outer loop):
for c in text:
so for the first iteration, the c
variable will have a value of "m"
.
then we loop through all the possible vowels, "m"
won’t equal any vowel, so c
variable will assign itself a value of "m"
over and over again. Then after the inner loop, append to t
variable (will contain result string)
then we get to second iteration of the outer loop, so c
variable will get a value of "e"
the "e"
will equal a vowel from the vowel string ("ieaouIEAOU"
), so c
variable will become an empty string. Then c
variable will for the remaining iterations of the inner loop assign an empty string to itself. So that after the inner loop/nested loop, we can append (t=t+c
) an empty string to t
(variable containing the result)
the else clause is just completely redundant:
def anti_vowel(text):
t=""
for c in text:
for i in "ieaouIEAOU":
if c==i:
c=""
t=t+c
return t
print anti_vowel("hello book")
this is already better, we could do one better:
def anti_vowel(text):
t=""
for c in text:
for i in "ieaouIEAOU":
if c==i:
c=""
break
t=t+c
return t
print anti_vowel("hello book")
no need to make more iterations once determined c
variable contains a vowel
but honestly, python allows to write such elegant code:
def anti_vowel(text):
result = ""
vowels = "ieaouIEAOU"
for char in text:
if char not in vowels:
result += char
return result
print anti_vowel("hello book")
its almost like reading english, if char not in vowels
, you read that, you instantly understand what the line does. By using python simple not in
operations + logic variable names, it makes such a difference
i would prefer to use lists for result
, but you get the idea that this problem has a much more elegant way to be solved