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


#1




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'):
            list(text).remove(i)
    return ''.join(list(text))
    .


#2

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.

Essentially,

for i in text:

and,

    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
        result.append(i)

Now we can return the joined result

return ''.join(result)

#3

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