Deleting vowels problem Please Help 8. anti_vowels


#1

I have been trying to solve this section but nothing is working. I have been stuck in this one for days now. So can someone please help me?

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

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

def anti_vowel(text):
    for i in text:
        if i in "aeiouAEIOU":
            new=text.replace(i,"")
          
    return new

    
   
print anti_vowel("Hey look Words!")

#2

we can see this problem nicely by doing:

def anti_vowel(text):
    for i in text:
        if i in "aeiouAEIOU":

            new=text.replace(i,"")
            print new
    return new

    
   
print anti_vowel("and the this do truck")

run this code, see where i added a print statement because text is unaffected, only the last replace action persist.


#5

I see your point but i tried it but it is still giving me errors

Error:
Oops, try again. Did you create a function called anti_vowel? Your code threw a “local variable ‘new’ referenced before assignment” error.

Traceback (most recent call last):
File “python”, line 11, in
File “python”, line 4, in anti_vowel
UnboundLocalError: local variable ‘new’ referenced before assignment


#6

Would you consider building a new string out of the characters that are _not_found in “aeiouAEIOU”?

new_string = ""
for c i text:
     if c not in aeiou:
          new_string += c

#7

I have absolutely no idea why, but it’s because you’re using the variable “new”. If you replace it with “text”, it in every position you have it, it works.

def anti_vowel(text):
    for i in text:
        if i in "aeiouAEIOU":
            text = text.replace(i,"")

    return text
print(anti_vowel("Hey look words!")

I tried tinkering with it to keep the “new” code, but I couldn’t find a way to keep it. The way I did it myself, though was:

def anti_vowel(text):
    vowels = 'aeiouAEIOU'
    for x in vowels:
        text = text.replace(x, "")
    return text
print anti_vowel('abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ')

#8

oops, that is sloppy of me. I fixed it, now you can run the code to see the problem


#9

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