Pratice makes perfect - 8. Anti-vowel


#1

Hey guys, could someone explain me why this code doesn’t seem to work? I think it might have something do with the for loops but I’m just can’t find the problem, still I think it’d be important for me to understand what’s wrong before moving on…

def anti_vowel(word):
  total = ""
  for c in word:
    for i in "aeiouAEIOU":
      if c==i:
        c = ""
      else:
        c = c
  total = total + c
  return total
  

print(anti_vowel("casateruila"))

#2

the biggest problem with your code is the else. We only want to do else after we determined c is not a vowel. When do we determine c variable currently doesn’t hold a vowel? After it successfully ran through the second loop

so else should be outside the second loop. And yes, we can have for/else like so:

for condition:
    print "do something"
else:
   print "something else"

now the else will only run when the loop condition becomes false, not when the loop breaks.


#3

I don’t think there is any problem with your else.
The problem is in line 9
“total = total +c” should not be outside the first for loop
add two spaces before that will be ok


#4

This topic was automatically closed 7 days after the last reply. New replies are no longer allowed.