Anti_vowel


#1

https://www.codecademy.com/courses/python-intermediate-en-rCQKw/1/2?curriculum_id=4f89dab3d788890003000096#

I get a funny error message for this:

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

It removes the first two "o"s but not the third. Any ideas?

def anti_vowel(text):
    listed = list(text)
    for letter in listed:
        if letter in "aeiouAEIOU":
            listed.remove(letter)
    concatenated  = "".join(listed)
    return concatenated

#2

the problem with this solution is index shift because you remove from the same list as you are looping over

the moment the first o of look gets removed, everything to the right of the removed character shifts one index to the left because lists can't have empty indexes, then the loop moves on to the next index, which causes problems

the solution would be not to loop over the same list as you are removing from, so you could loop over the string (text) while removing from the list


#3

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