#8 anti_vowel


#1




The code below worked perfectly fine when I tested it out with various strings, but when I put it to their test, it returns "Hy lk Words!" instead of Hy lk Wrds!. What is going on, and how do I fix it?


def anti_vowel(text):
    my_word = []
    for letters in text:
        my_word.append(letters)
    for char in my_word:
        if char == "a" or char == "e" or char == "i" or char == "o" \
or char == "u" or char == "A" or char == "E" or char == "I" or char == "O" \
or char == "U":
            my_word.remove(char)
    no_vowels = "".join(my_word)
    return no_vowels


#2

Removing an element from the current iterable will cause everything to shift left, and therefore skip the next element in the original sequence. That is one possible explanation. Always dicey when we attempt to mutate a list we are iterating at the time.

There are of course ways to do this successfully, so don't throw the baby out with the bath water. What happens if we iterate over the list in reverse order? That way the left shift is never felt.

One could also consider that there is an easier way than a ten operand logical expression. Remember in?


#3

Thank you for your help! While I was building this I also learned about the reversed function.

Here is my new code

def anti_vowel(text):
    my_word = []
    for letters in text:
        my_word.append(letters)
    for char in reversed(my_word):
        if char in "aeiouAEIOU":
            my_word.remove(char)
    no_vowels = "".join(my_word)
    return no_vowels

#4

There is a built-in for this, list()

str = "this is text"
print list(str);
# ['t', 'h', 'i', 's', ' ', 'i', 's', ' ', 't', 'e', 'x', 't']

Is this actually working for you? (On testing I see that it does.) I was thinking more along the lines of a reverse range (right to left).


#5

Another way that works

def anti_vowel(text):

vocals = ['a','e','i','o','u']
vocals_up = ['A','E','I','O','U']
new_text = []
for i in text:
    if i not in vocals_up:
        if i not in vocals:
            new_text.append(i)
            print new_text



no_vowels="".join(new_text)

print no_vowels
return no_vowels

print anti_vowel("HOLA")


#6

Shows a grasp of concepts, perhaps; but, also serves as a model of how not to do it.


#7

I'm just learning :slight_smile: What issues do you see in my code?


#8

The above is the simplest form of iterable, which common sense would suggest is the one to use in the test. We don't need two lists and nested if statements. We are given to assume that the print statements are for debugging.


#9

Sure! I should have used one list, simplifies everything

Also, yeah I left that print statement behind.

Thanks so much for your help!


#11