Anti_Vowel not catching all instances of a letter


#1



def anti_vowel(text):
    letters = list(text)
    vowels = ["o", "O", "a", "A", "e", "E", "i", "I", "u", "U"]
    for p in letters:
        if p in vowels:
            letters.remove(p)
    
    print "".join(letters)
    return "".join(letters)

This returns "Hy lk Words!" as opposed to the expected "Hy lk wrds!"

It seems as if once the program removes the first couple of Os in "look", it disregards the ones that follow. How do I stop that happening?


#2

This is the issue. Using .remove in a loop can result in errors since it interrupts the iteration. Better in this instance to build a new list from the non-vowels in text.

Also, since a string is iterable, we don't need a list.

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

#3

One long winded way of doing it :joy:

def anti_vowel(text):
    vowelless = ""
    for letter in text:
        if ord(letter) == 97 or ord(letter) == 101 or ord(letter) == 105 or\
ord(letter) == 111 or ord(letter) == 117 or ord(letter) == 65 or \
ord(letter) == 69 or ord(letter) == 73 or ord(letter) == 79 or \
ord(letter) == 85:
            vowelless += ""
        else:
            vowelless += letter
    return vowelless

#4

And one short-winded way...

def anti_vowel(text):
    return "".join([p for p in text if p not in "aeiouAEIOU"])
    
print anti_vowel("the quick brown fox jumps over a lazy dog")

# th qck brwn fx jmps vr  lzy dg

#5

Excellent lesson to make such kind of memes, lol