Welcome to the Treehouse Community

Want to collaborate on code errors? Have bugs you need feedback on? Looking for an extra set of eyes on your latest project? Get support with fellow developers, designers, and programmers of all backgrounds and skill levels here with the Treehouse Community! While you're at it, check out some resources Treehouse students have shared here.

Looking to learn something new?

Treehouse offers a seven day free trial for new students. Get access to thousands of hours of content and join thousands of Treehouse students and alumni in the community today.

Start your free trial

Python Python Collections (2016, retired 2019) Lists Disemvowel

Ján Pokorný
Ján Pokorný
1,031 Points

Can't figure out why my code doesn't work

When I run the code in pyCharm, it works every time. The only difference is I ask for an word through word = input("Type in a word: ).

Thank you for your answers!

disemvowel.py
def disemvowel(word):
    vowels = ["a", "e", "i", "o", "u","A", "E", "I", "O", "U"]
    word_list = list(word)

    for letter in word_list:
        if letter in vowels:
            try:
                word_list.remove(letter)
            except ValueError:
                break
    word = "".join(word_list)
    return word

1 Answer

Hi there,

While your code does work for the most part, the reason it won't pass is because it won't work properly on a word with multiple vowels in a row (as a quick example, if you put in 'oops' it will give back 'ops'). This is because you are looping through the same iterable that you are removing values from in the loop, which can cause issues (because it's throwing off the positioning of the items in the iterable). A quick solution to this is to loop through word instead of the list:

Here is your code with one small adjustment:

def disemvowel(word):
    vowels = ["a", "e", "i", "o", "u","A", "E", "I", "O", "U"]
    word_list = list(word)

    for letter in word:  # changed to loop through the string instead of the list
        if letter in vowels:
            try:
                word_list.remove(letter)
            except ValueError:
                break
    word = "".join(word_list)
    return word

Hope this helps!

Ján Pokorný
Ján Pokorný
1,031 Points

thanks a lot! code does work now