Anti_vowel


#1



anti-vowel


Can anyone tell me why this code doesn't work? (I know it's very different from the intended way of solving this exercise - I'm trying out an alternative approach). The code does the weirdest thing: it removes the first two 'o's from 'look', but not the 'o' from 'Words!'


I can't see why, if the for-loop goes over each character individually, it fails to remove the last vowel.

Thanks in advance for any help!


Replace this line with your code. 
def anti_vowel(text) :
    lett_list = []
    var = ""
    for each_lett in range(0, len(text)) :
        lett_list.append(text[each_lett])
    for each_item in lett_list :
        if each_item == "o" :
            lett_list.remove(each_item)
        elif each_item == "i" :
            lett_list.remove(each_item)
        elif each_item == "e" :
            lett_list.remove(each_item)
        elif each_item == "a" :
            lett_list.remove(each_item)
        elif each_item == "u" :
            lett_list.remove(each_item)
    for each_thing in lett_list :
        var = var + each_thing
    return var


#2

Hi @ai223,

You are changing the size of lett_list while you iterate through it. For example, this statement reduces the number of items in the list by one ...

lett_list.remove(each_item)

That interferes with the iteration process by shifting the positions of some of the items in the list.


#3

2 posts were split to a new topic: Problem with anti_vowel


#4

def anti_vowel(text):
    return "".join([char for char in text if not char in "aeiouAEIOU"])

Well, It is not good to use "for ... in ..." as that way. Please read my code.


#5

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