def anti_vowel(text):
result = “”
for i in text:
if i == ‘A’ or i == ‘a’ or i == ‘E’ or i == ‘e’ or i == ‘o’ or i == ‘O’ or i == ‘U’ or i == ‘u’:
result += " "
else:
result += i
return result
def anti_vowel(text):
for char in text:
if char not in "aeiouAEIOU":
print char,
anti_vowel("dog eat dog") #prints out "d g t d g"
The result should be acceptable, since it does remove the vowel but the site doesn’t seem to like this solution. I wonder why? Is it because of the spaces between the printed characters?
I’m not quite sure why my code doesn’t work? It seems to remove all vowels except for the one “o” in “Hey look words!” I’m not necessarily looking for a solution, I just want to know why my code doesn’t work. Thanks for any help.
removing from the same list as you are looping over, is a bad idea
lists can’t have empty spots/slots, so the moment you remove something from the list, everything to the right of the removed item shifts one space to the left
combined with the loop you are using (which moves to the next index), this causes letters to be skipped
vowel = ["A", "E", "I", "O", "U", "a", "e", "i", "o", "u"]
def anti_vowel(text):
for t, v in zip(text, vowel):
if t == v:
text.remove(t)
print anti_vowel("Happy")
Hi all, thank you very much for the sharing. While I have seen how others approached the problem, it’s still not quite clear to me where I am getting wrong, the terminal delivers ‘None’. Please can somebody help? Thank you.
Hi, I wrote a similar code as well and got the same output. I’m not sure I quite understand your explanation. If the loops skips the second ‘o’, wouldn’t it print 'Hy lok Wrds!" instead of ‘Hy lk Words!’? Thanks!
Consider how str.remove works. It does not remove the character at the index, but the first one it encounters from left to right. The last o is the one that will remain even though it was the earlier one that was skipped over.