Hello everyone,

Here is the my code for this exercise. It works halfway through the input, the input is "Hey look words!", but the output is "Hy lk words!". Can anyone help? I just don't get why it works for the first two words but doesn't work on the third one.

def anti_vowel(text):
    text = str(text)
    letters = list(text)
    for x in letters:
        if x in "aeiouAEIOU":
    return "".join(letters)

print anti_vowel("Hey look Words!")


You're iterating through a list, and while doing so, you remove elements from it.

Could changing the length of the list you are currently iterating through cause trouble?

Also note that the remove method might not be what you want since it takes time relative to the length of the string (first to find a value equal to the one specified, and then move all values that come after to fill the now-empty spot) - that's not how you would be doing it manually because it's really inefficient, so it's probably not what your code should do either (although it won't be noticeable for strings that are just a few hundred characters)


Follow the solution my colleague

def anti_vowel (text):
aux = ""
for i in text:
if i not in "aeiouAEIOU":
aux += i
return aux
text= "Hey You!"


you don't have to use lists at all.

Check my solution:

def anti_vowel(text):
  vowels = ["a", "e","i","o", "u"]
  new_text = ''
         for i in text:
             if i.lower() not in vowels:
             new_text += i
         return new_text

so, just create new empty string, and later check if any of the items (characters) from 'text' argument IS NOT any of the vowels. if character is not vowel, add him to newly created string. if character is vowel, the loop won't do anything, it will go on to the next item of the 'text'.

also, note the lower() method on each character from text argument. it's better to modify what you want to evaluate than to have a container with all variations of data, because if the task was the oposite - to remove all consonants, would you add all B, C, D, F, G, H, J, K, L, M, N, P, Q, R, S, T, V, X, Z and b,c,d,f,g,h etc..? :slight_smile:

using lower() you change the character to lower case so you can match it with vowels list. note that doesn't change the "i" that you later pass to new_text. it's fine for this case where we have 10 characters, but just so you know for future, where you could have many varitations.


Well, a much much simpler option is to follow the code below:

> def anti_vowel(text):
>     for i in "aeiouAEIOU":
>         text = text.replace(i,'')
>     return text

vowel gets removed from the text each time the loop iterates through aeiouAEIOU


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