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 trialTobias Edwards
14,458 PointsMy 'disemvowel' function isn't passing the challenge?
Question OK, I need you to finish writing a function for me. The function disemvowel takes a single word as a parameter and then returns that word at the end. I need you to make it so, inside of the function, all of the vowels ("a", "e", "i", "o", and "u") are removed from the word. Solve this however you want, it's totally up to you! Oh, be sure to look for both uppercase and lowercase vowels!
Anyone know why it isn't passing the challenge? I've tested the function and it seems to work fine.
Thanks
def disemvowel(word):
word = list(word)
for letter in word:
if letter.lower() in ["a", "e", "i", "o", "u"]:
word.remove(letter)
else:
continue
return "".join(word)
1 Answer
Steve Hunter
57,712 PointsHi Tobias,
You'll get indexing issues if you use a for
loop and remove elements from the iterable being looped over. For example, if you loop over the word STEVE
the first iteration will be holding the S
. If you then remove that, the second iteration will move to the now second element, i.e. the first E
, it'll omit the T
entirely.
1 2 3 4 5
S T E V E
# first iteration holds S
Delete S in the first iteration:
1 2 3 4
T E V E
# second iteration holds E
For this challenge, it is easiest to build up an output string of non-vowels by using not in
, I found.
I created a blank string at the top of the method and appended to that any letter.lower()
that was not in
the list of vowels. Once the loop was completed, I returned that string.
Let me know how you get on.
Steve.
Tobias Edwards
14,458 PointsTobias Edwards
14,458 PointsWow! I changed my code and it worked :) Thank you Steve for the clear and in-depth response
Steve Hunter
57,712 PointsSteve Hunter
57,712 PointsGlad you got it working!