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 trialBenjamin Jackowitz
2,082 PointsDisemvowel - Getting right answer but no dice
Tried the following, which returns the right string, but I'm getting an error that says "got back letters I wasn't expecting."
What should I be doing differently?
def disemvowel(word):
vowels = ["a","e","i","o","u"]
word_list = list(word.lower())
ind=0
for letter in word:
if word_list[ind] in vowels:
del word_list[ind]
word = ''.join(word_list)
else:
ind += 1
return word
3 Answers
Chris Freeman
Treehouse Moderator 68,441 PointsBy lowering the entire word, non-vowels are also lower-cased. This is not desired.
Instead of lowering the whole word
, lower only the letter being compared. With this simple switch, your code passes.
def disemvowel(word):
vowels = ["a","e","i","o","u"]
word_list = list(word) # <-- removed .lower() from here
ind=0
for letter in word:
if word_list[ind].lower() in vowels: # <-- added .lower() here
del word_list[ind]
word = ''.join(word_list)
else:
ind += 1
return word
Steven Parker
231,269 PointsThere's some unusual aspects to this code worth mentioning.
While the code works (with Chris's modification) there's some rather strange things going on here. For one, you iterate using word but then modify word inside the loop. Normally doing this kind of thing would cause elements to get skipped over.
But on the other hand your iterator is lettter, but letter is never used in (or after) the loop. Not illegal but very peculiar! I guess in this case two potential problems actually cancel each other out!
So while you pass the challenge, you also point out two good programming practices (by violating them both!):
- never modify the source of iteration inside the loop
- if you create but never use an iterator, you should probably use a different kind of loop
I'd be inclined to give you a prize for cleverness along with an admonition of "don't do that again!"
Steven Parker
231,269 PointsNote that Chris's 2nd mod fixes item 1.
Benjamin Jackowitz
2,082 PointsChris, thank you! I was able to figure it out.
Steven - Understood on both points, and appreciate the feedback.
Thanks for the speedy responses
Chris Freeman
Treehouse Moderator 68,441 PointsChris Freeman
Treehouse Moderator 68,441 PointsAdditionally, there is no need to rejoin word in each loop. The way that the code
del
characters from theword_list
, the resulting rejoined would be equivalent to the remaining indexed characters fromword
in thefor
statement. So thejoin
can be move to the end and run once thus making the code more efficient.