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 trialWilliam Bailey
4,585 PointsHelp with this for loop.
def disemvowel(word): vowels = 'aeiouAEIOU' result = '' for letter in word: if letter in vowels: result = word.replace(letter, '') return result
I'm losing my sanity with this one?
it will proof when passed 'here' but when passed something like 'placeit' , no good?
can anyone explain this to me?
def disemvowel(word):
vowels = 'aeiou'
result = ''
for letter in word:
if letter in vowels:
result = word.replace(letter, '')
return result
1 Answer
Maurice Abney
Courses Plus Student 9,859 PointsThe trick to this solution lies in the fact that strings are immutable(cant change) but lists are mutable(ie, do work on list and then convert to string). What's being returned from each of your replace() calls is a new string acting on the original word, its not updated at all, so your end result will simply be the original word with the 'a's removed, e's i's o's and u's intact. reason being, after the first replace, the result is returned. A similar solution could be done with recursion by calling returning disemvowel(result) and changing a few lines but that's another discussion. You dont need recursion to solve this problem.
Here's my solution:
def disemvowel(word):
result = [] # empty list
for letter in word:
if letter.lower() not in "aeiou":
result.append(letter)
return ''.join(result) #join list back into string
William Bailey
4,585 PointsWilliam Bailey
4,585 PointsThank you very much, i had tried this prior to coming up with what is posted however, in the last line of code tried to use str(result) instead of ''.join() > joining the list into a string which did not work. I really like the sound of this recursion technique though, thank you for pointing that out. Always worth finding more than one way to do something.