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!
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
Help 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
Maurice AbneyCourses Plus Student 9,859 Points
The 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