"Hy lk Words!" - extra 'o'?


#1



https://www.codecademy.com/courses/python-intermediate-en-rCQKw/1/2


My code removes all of the vowels except one of the 'o's at the end even though it has removed all the other 'o's in the rest of the text. This is my error message - Your function fails on anti_vowel("Hey look Words!"). It returns "Hy lk Words!" when it should return "Hy lk Wrds!".


I can't work out why it won't remove the 'o' in 'Words'


vowels = ('a', 'e', 'i', 'o', 'u', 'A', 'E', 'I', 'O', 'U')

def anti_vowel(text):
    text = list(text)
    for c in text:
        for v in vowels:
            if c == v:
                text.remove(c)
    text = ''.join(text)
    return text


#2

because of index shifts, you remove from the list you are looping over, this means if there are two consecutive vowels, like in look, the second vowel gets skipped.


#3

But the extra vowel is in Words which doesn't have consecutive vowels.


#4

so the second o in look gets skipped, but then the loop arrives at the o of words, but remove() will remove the first o it can find, which is the one your for loop skipped in look

If you don't know the behavior of the built in functions you are using, it can indeed let you to believe the problem is somewhere else then you think.

Remove is a difficult approach to solve this problem


#5

Makes sense - thank you!


#6

Do you think you can work out a solution, or do you need more help?


#8

I have the same problem here, and could you give some advice to the solution?


#9

There are multiply solutions possible, remove() is possible but not recommend, it is quit a difficult approach.

make an empty list, loop over text and append everything that is not a vowel to your list. After the loop, join the list into a string using .join()

If you need more help, make a new topic


#10

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