Anti_vowel


#1



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

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


def anti_vowel(text):
    vowels = ['a', 'e', 'i', 'o', 'u', 'A', 'E', 'I', 'O', 'U']
    word = []
    final = ""
    for i in text:
        word.append(i)
    for i in word:
        if i in vowels:
            ind = word.index(i)
            del word[ind]
    for a in word:
        final = final + a
    return final
print anti_vowel('Hey look Words!!')


#2

 for i in word:
        if i in vowels:
            ind = word.index(i)
            del word[ind]

This block of code is actually creating that bug.
Reason: When you loop over your list word and delete items from it,its length changes and some of the vowels are left un removed.

Create a copy of list..
say
word2 = list(word)

and delete items from it while looping over word
when you do this you have to make a change in last for loop too.
as your changed version of list with no vowels is word2.

I hope it helps!


#3

Thanks it helped a lot


#4

def anti_vowel(text):
vowel= "aeiouAEIOU"
word=""
for n in text:
if n not in vowel:
word+= n
return word


#5

Think this is the most simple and easy to understand method of doing it :slight_smile:


#6

True !!! i don't believe in making the code big or complicated..


#7

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