8. I am stuck


#1

Practice makes perfect unit has been tough for me and nearly had to reference the forum for nearly all of the lesson before 8. I tried this one myself and it feels like I'm close but still feels like I don't get it.

def anti_vowel(text):
    new = ""
    for c in text:
        if c == "a" or c == "e" or c == "i" or c == "o" or c == "u":
         remove = text.replace(c, "")
         new = new + remove
    return new
print anti_vowel("Hey look Words!")

This is the code I came up with but when I test it, it spits the error

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

Anyone that can point me in the correct direction would be greatly appreciated, I really don't want to copy and paste someone's code again because it feels like I'm not learning anything


#2

@prodigy97 take a look at the code excerpt below i have comment on what changed

def anti_vowel(text):
    new = ""
    for c in text:
        #if c == "a" or c == "e" or c == "i" or c == "o" or c == "u":
        #lets shorten the work here and use this
        #=====================================================
        #when we get to each character we convert it to 
        #lowercase and check it against our vowels
        if c.lower() in "aeiou":
            #remove = text.replace(c, "")
            #instead of replace
            #we will just not add it to the new string
            continue
        new = new + c
        
    return new
print anti_vowel("Hey look Words!")

we can further shorten your code with this

def anti_vowel(text):
    new = ""
    for c in text:
        #if c == "a" or c == "e" or c == "i" or c == "o" or c == "u":
        #lets shorten the work here and use this
        #=====================================================
        #when we get to each character we convert it to 
        #lowercase and check it against our vowels
        if c.lower() not in "aeiou":
            #we will just add what character
            #we are interested in to our new string
            new = new + c
    return new
print anti_vowel("Hey look Words!")

#3

Thanks a lot, makes much more sense.