Anti_vowel


#1



https://www.codecademy.com/en/courses/python-intermediate-en-rCQKw/1/2?curriculum_id=4f89dab3d788890003000096


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


I have no idea why the loop is missing the "o" in "Words" when it managed to remove the ones in "look". Sorry for taking your time up, it's just that I've looked around and couldn't find anyone else with the same problem.


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


#2

for i in text:

This is a look-up loop. No changes will be made to the list. To mutate list elements inside a loop we need to use range() but that brings a different issue... When an element is removed from an iterated list, the range does not change so the iteration goes out of bounds.

In short, we should avoid removing elements from a list while iterating. Create a new empty list and append valid characters to that list. Then it is a straight forward look-up.

You should also note that a text string is already an iterable so doesn't need to be converted to a list.


#3

Thanks for the help, but there's one thing I still don't understand. I know that strings can be iterated through, but I receive an error when I type, say stringName.append(i). How do I work around that?


#4

We can iterate the string, but we cannot append to a string, only a list or tuple. Create an empty array before the loop, then append to it.


#5

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