Anti_vowels


#1




Your function fails on anti_vowel("Hey look Words!"). It returns "Hy lk Words!" when it should return "Hy lk Wrds!".


I expected the function to remove all vowels, however it returns "Hy lk Words!" instead of "Hy lk Wrds!" which is completely baffling.


def anti_vowel(text):
    vowels = list("aeiouAEIOU")
    text = list(text)
    for c in text:
        for i in vowels:
            if i == c:
                text.remove(c)
    
    print ''.join(text)
    return ''.join(text)
    
anti_vowel("Hey look Words!")


#2

Did you figure it out? Running into the same error myself.

Edit:

def anti_vowel(text):
text = list(text)
while "a" in text:
for letter in text:
if letter in "aeiouAEIOU":
text.remove(letter)
while "e" in text:
for letter in text:
if letter in "aeiouAEIOU":
text.remove(letter)
while "i" in text:
for letter in text:
if letter in "aeiouAEIOU":
text.remove(letter)
while "o" in text:
for letter in text:
if letter in "aeiouAEIOU":
text.remove(letter)
while "u" in text:
for letter in text:
if letter in "aeiouAEIOU":
text.remove(letter)
while "A" in text:
for letter in text:
if letter in "aeiouAEIOU":
text.remove(letter)
while "E" in text:
for letter in text:
if letter in "aeiouAEIOU":
text.remove(letter)
while "I" in text:
for letter in text:
if letter in "aeiouAEIOU":
text.remove(letter)
while "O" in text:
for letter in text:
if letter in "aeiouAEIOU":
text.remove(letter)
while "U" in text:
for letter in text:
if letter in "aeiouAEIOU":
text.remove(letter)
return "".join(text)

^This works, just not pretty.


#3

You're removing values from the list that you're iterating through - this affects what characters that the loop will visit


#4

I have tried to modify the code to get it to stop removing items from the list by adding to a new list when it doesnt match the vowel list but it is not working as i expected.

Can you please help me out which explaining why.
I put in the range(len(vowels)) as i want to check anything in the list
i also tried to use instead of that line:
if i == c in vowels:
this also did not work, I thought python knows c is character so i shouldnt have to define it.

def anti_vowel(text):
vowels = list("aeiouAEIOU")
text = list(text)
new_text = []
for i in text:
        if i == range(len(vowels)):
            new_text.append("")
        else:
            new_text.append(i)

print ''.join(new_text)
return ''.join(new_text)

anti_vowel("Hey look Words!")


#5

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