Anti-Vowel - Loop don't detect all vowels


#1


anti_vowel

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

Shouldn't it iterate through all the vowels? Why did it miss the 'o' in 'Words'?


def anti_vowel(text):
    vowels = ['a','e','i','o','u']
    text = list(text)
    for vowel in vowels:
        for char in text:
            if vowel == char or vowel.upper()==char:
                text.remove(char)
    return "".join(text)


#2

@steven_steven

Check out a post that is still new.


#3

index shift is happening, already suspected it. The drawback of remove methods

okay, in your for loop, at certain point vowel is o, make sense? right, then it is going to the second for loop:

for char in text:

then it is arriving at the word look. look has two o's, when it arrives at the first o, it sees an o, and removes it. But now the second o shifts to the position of the first o, but your second loops moves on to the next letter, ignoring the shifted o.

Then it arrives at words, find an o and remove will remove the first instance of o it can find, which is in lok (look with a removed o), so then you have lk, but the o in words remain

You either need to build something to prevent this index shifting, or another loop to verify.

Or, you could just go for the simple solution and append constants to a new list.


#4

3 posts were split to a new topic: [solved] anti-vowel


#5

A post was split to a new topic: Anti vowel