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

kelsang sherab
kelsang sherab
749 Points

Stuck with an Error

When I check the code I wrote for the challenge I get the following Error: Bummer: SyntaxError: invalid syntax (disemvowel.py, line 15)

Is the code wrong? Or do I have an issue with the return line I have tried the code without the return but then I get EOF error

I am a little stuck...

disemvowel.py
word = print(input("enter a word:\n >   "))

def disemvowel(word):
    letters = list(word)
    for letter in letters:
        try:
            letters.remove('a')
            letters.remove('e')
            letters.remove('i')
            letters.remove('o')
            letters.remove('u')
        except ValuError:
            pass
        word = ("".join(letters)
       return(word)

3 Answers

Øyvind Andreassen
Øyvind Andreassen
16,840 Points

Hey there are some typos in your code.

The syntax error is from you calling ValuError not ValueError. You are missing a closing parenthesis in word = ("".join(letters). Also, you are wrapping the return value in parenthesis that isn't necessary.

More of a coding thing, you are returning within the for-loop, which you don't want in this instance. You also have an issue with your indenting, which Python will throw a fuzz about. word = ("".join(letters) is indented with one additional space.

Solution:

def disemvowel(word):
    for letter in word:
        if letter in ["a", "e", "i", "o", "u", "A", "E", "I", "O", "U"]:
            word = word.replace(letter, "")
    return word

Let's talk about what is happening. You can loop through strings so no need to separate it out into a list. We also have a list of characters that we want to remove, both upper- and lowercases. Then we call the .replace() function on the string to remove the specific letter by replacing it with nothing.

kelsang sherab
kelsang sherab
749 Points

Yeah - makes sense very elegant solution thanks for sharing

Hello @kelsang sherab it seems you you have an extra parenthesis in your final 'word' assignment (line 15). I think it should look like ''' word = "".join(letters) '''. Hope this help point you in the right direction.

kelsang sherab
kelsang sherab
749 Points

that's so silly - thanks for this.

This may not pass the challenge but but it should get rid of your syntax error. I notice you are removing the letters using the same line of code repetitively. Maybe you could try to iterate through a string of the vowels or something? Something like:

for letter in letters: 
    if letter in "aeiouAEIOU":
        letters.remove(letter)

Hopefully this will help.