Anti_vowel


#1

<PLEASE USE THE FOLLOWING TEMPLATE TO HELP YOU CREATE A GREAT POST!>

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

idk why my code is unable to remove the last vowel even though it works fine with the words before it.
error message:Oops, try again. Your function fails on anti_vowel(“Hey look Words!”). It returns “Hy lk Words!” when it should return “Hy lk Wrds!”.


here is my code:
def anti_vowel(text):
    result = []
    for i in text:
        result.append(i)
    for a in result:
        if a in "aeiouAEIOU":
            result.remove(a)
    return ''.join(result)



#2

result.remove(a) removes the first instance of whatever a is.
the problem is your removing things from a list that you are currently iterating. this causes you to skip items because the iterator moves up by one but all of the items just moved down one position, specifically the second ‘o’ in look. then you hit the ‘o’ in words and it removes the first instance of ‘o’ which is the skipped one in look.


#3

so what should i change to make my code work


#4

I would recommend doing something like this:

def anti_vowel(text):
    result = ""
    for i in text:
        if i not in "aeiouAEIOU":
            result = result + i
    return result

#5

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