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 trialNick Lenzi
7,979 Pointsdisemvowel challenge: My code works perfectly yet the challenge won't take it..?
It works perfectly when I run it an IDLE. Even ran it in a debugger to ensure the return value is proper but for some reason the challenge submit won't accept it saying "hmm, got back letters I wasn't expecting?" Am I missing something obvious or is the challenge being picky again. I mean i don't see why it should because it says I can solve it however I want sooooo...
def disemvowel(word):
vowels = ["a", "e", "i", "o", "u", "A", "E", "I", "O", "U"]
word = list(word)
for letter in word:
if letter in vowels:
word.remove(letter)
word = ''.join(word)
return word
3 Answers
Stuart Wright
41,120 PointsRemoving elements from a list as you iterate over it causes issues. Have you tested your code by passing various different words to it? How about one which has two vowels in a row?
A better way would be to create a new blank list and append non-vowels to it as you iterate over the original word.
Steven Parker
231,236 PointsIt may only work "perfectly" with some words.
Try a word like "bookkeeper".
There's a problem with using something as the basis for iteration and then altering it inside the loop. That can cause some items to be skipped over. Some ways to avoid that would be to use a copy of the item for iterating, or to build a new item to be returned. The first method is probably easiest and can be done using a slice:
for letter in word[:]:
Nick Lenzi
7,979 Pointsok that is definitely useful knowledge I probably would not have learned without you. Thanks! I love breaking down the bare bones of how a process works.
Nick Lenzi
7,979 PointsNick Lenzi
7,979 Pointsjeeeeez i didn't even think to test repeat vowels thank youuuu. I'm curious what you mean by removing elements from a list while iterating over it causing issues though. Could you maybe go a little more in depth on this?
Stuart Wright
41,120 PointsStuart Wright
41,120 PointsIf you imagine looping over the word 'four', this is what your original program would have done: