Tell me the right code, and the mistakes in the current one


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

def anti_vowel(text):
    for  i in list(text):
        if i in list('aeiouAEIOU'):
    return ''.join(list(text))


The list() function is handy for converting a string to a list, if we have a desire to mutate the string. If we are simply looking up values in the string, then it is enough that it is already an iterable.


for i in text:


    if i in "aeiouAEIOU":

are sufficient for our needs.

Removing items from a list within a loop can upset the iteration process by skipping elements. This is not a recommended approach unless one has carefully throught it through.

Using just the string, and an empty array we can iterate over the string, and capture (append) valid characters (consonants) to the array, and skip over the vowels.

result = []
for ___:
    if i not in ___:        # note the NOT in that statement

Now we can return the joined result

return ''.join(result)


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