8. Anti-Vowel - my code is ignoring "e"


#1

My code is below:

def anti_vowel(text):
    new_text = []
    for letter in text:
        letter.lower()
        if letter == "a":
            new_text = text.replace("a","")
        elif letter =="e":
            new_text = text.replace("e","")
        
        elif  letter =="i":
            new_text = text.replace("i","")
        elif letter =="o":
            new_text = text.replace("o","") 
        elif letter == "u":
            new_text = text.replace("u","")
        else:
            print letter,
            print "isn't a vowel!",
    return new_text

No error messages show up in the window, but I get the response "Oops, try again. Your function fails on anti_vowel("Hey look Words!"). It returns "Hey lk Wrds!" when it should return "Hy lk Wrds!". --- In effect, my program is ignoring the letter "e." I've tried moving that if statement, I've tried retyping it, I can't figure out why it's getting ignored.


#2

Woah! That is a lot of code, that can be shortened...

Use this:

def anti_vowel(text):
    word = ""
    for i in range(0 , len(text)):
        if text[i] not in "aeiouAEIOU":
            word = word + text[i]
    return word
    w = raw_input("enter a word: ")
    print anti_vowel(w)

You can also try rubber duck debugging. I use it a lot and it seems a bit stupid but it really helps. Just read your code to a toy or rubber duck. Then it actually 'shows' your bug!


#3

Thanks! That works (and I can see how/why), but can you see why my code didn't work? With different words it seemed to ignore different vowels.


#4

Since there is no mutation of the original, we can use a look-up loop.

result = []
for letter in text:        
    if letter not in "aeiouAEIOU":
        result.append(letter)
return "".join(result)

#5

Hi All,

I have a doubt in below code

def anti_vowel(text):
a = []
for i in text:
a.append(i)

for j in a:
    if j in "aeiouAEIOU":
        a.remove(j)
        a.append(" ")
        print(a)
k = ""
for l in a:
    k = k+l
return k.strip()

anti_vowel("Hey look Words!")

the output I am getting is - "Hy lk Words!"
Don't know why is it not detecting the last "o"
can anyone please help?


#6

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