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

Dominic Wojciechowski
seal-mask
.a{fill-rule:evenodd;}techdegree
Dominic Wojciechowski
Python Web Development Techdegree Student 1,726 Points

disemvowel: returns with "bummer" message

Evening all,

I keep an error message for the below code.

The things is, I've tried it out on Workspace and it works fine: If the word is "hello" it prints out "hll".

Any help would be much appreciated,

Dom

disemvowel.py
word = "hello"

word_list = list(word)

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

    for letter in word_list:
        if letter in vowels:
            word_list.remove(letter)
        else:
            continue

    word = str("".join(word_list))
    return (word)

4 Answers

Ryan S
Ryan S
27,276 Points

Hi Dom,

There are a couple things to sort out. First, the challenge provides you with a function definition and the returned variable. You shouldn't modify the function definition by changing the argument name.

Secondly, all code should be inside the function. It should be self-contained such that the only thing needed is the function argument to be passed in. And you don't need to define "word". The code checker will pass that in for you. You don't know what it will be, only that it will be a string that contains uppercase and lowercase letters.

And lastly, your logic is pretty good and you are on the right track. But I'd suggest testing your code with a word that has multiple vowels in a row. You'll find that it won't remove all the letters. This is because you are modifying a list (removing items) as you are looping through it. I'd suggest looping through a copy of the list, but still modifying the original, or vice versa.

Let me know if you have any questions. Good luck.

james south
seal-mask
.a{fill-rule:evenodd;}techdegree seal-36
james south
Front End Web Development Techdegree Graduate 33,271 Points

try it with a word like beauty that has consecutive vowels. the problem is you are modifying a list while looping through it. the remove method removes the given element, then shifts the remaining elements to the left. the loop moves on though, so elements get skipped, which is why a word like beauty will not have all the vowels removed. to fix make a copy and loop through that while modifying the original, or vice versa. also i would move the casting of the word into a list into the function body, so that it takes a word as its argument. this more properly encapsulates what the function does - take a word, remove the vowels, return the disemvoweled word.

jacksonpranica
jacksonpranica
17,610 Points

James South is correct in his thinking. I was wondering what was wrong here, but I remember when I did the challenge rather than making a copy of the list, i decided to just add each letter that wasn't a vowel to a string and then returned that string. I do believe that worked, can't remember if it's still the way.

matthew bernheimer
matthew bernheimer
10,254 Points

I am a beginner but I think it's because disemvowel() wants a string and you are passing it a list. Try making disemvowel take a word and move word_list = list(word) into the disemvowel() function.

Dominic Wojciechowski
seal-mask
.a{fill-rule:evenodd;}techdegree
Dominic Wojciechowski
Python Web Development Techdegree Student 1,726 Points

Thanks for all your input,

I've managed to get it sorted thanks to your guys help plus I've learned a new thing about for loops.

All the best,

Dom