Anti_vowel HELP!


#1



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


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


I dont understand why it's not taking out the "o" in words. When I try changing the character to a different vowel, it works.


total = []
def anti_vowel(text):
    total = list(text)
    print total
    for i in total:
        if i == "a" or i == "A":
            total.remove(i)
        elif i == "e" or i == "E":
            total.remove(i)
        elif i == "i"or i == "I":
            total.remove(i)
        elif i == "o" or i == "O":
            total.remove(i)
        elif i == "u" or i == "U":
            total.remove(i)
    print total
    new_total = ""
    for i in total:
        new_total += i
    return new_total
    print new_total
            

print anti_vowel("Hey look Words!")


#2

the problem is actually in look, the problem with remove() is that index shift occurs (string can't have empty indexes so everything to the right of what you removed shifts one index to the left)

couple of ways to solve this problem:

loop over text while removing from total

loop over in reverse

append constants to new list/string


#3

Okay, so I changed the code so it appends to a new variable.

Here is my new code:

final = []
def anti_vowel(text):
    total = list(text)
    print total
    for i in total:
        if i != "a" and i != "e" and i != "i" \
        and i != "o" and i != "u":
            final.append(i)
    print final
    new_total = ""
    for i in final:
        new_total += i
    return new_total
    print new_total
            
print anti_vowel("Hey look Words!")

however, I am now getting this error message: Oops, try again. Your function fails on anti_vowel("Hey look Words!"). It returns "Hy lk Wrds!bcdHy lk Wrds!" when it should return "Hy lk Wrds!".

I don't even understand where it is getting bcd.

In the console however, it is printing the correct answer: Hy lk Wrds!


#4

the exercise needs to verify your code function works correctly, this is done by calling the function (not the whole script), because you declare final outside your function, final will not be set to an empty lits again when you call the function multiply times


#5

ahh okay thank you. That makes more sense. Ugh these little things are so frustrating!!

Here's the code that actually works (i had to add the capitalized vowels and change the placement of final = []):

def anti_vowel(text):
    total = list(text)
    final = []
    print total
    for i in total:
        if i != "a" and i != "e" and i != "i" \
        and i != "o" and i != "u" and i != "A" \
        and i != "E" and i != "I" and i != "O" \
        and i != "U":
            final.append(i)
    print final
    new_total = ""
    for i in final:
        new_total += i
    return new_total
    print new_total
            
print anti_vowel("Hey look Words!")

#6

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