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 trialTor Ivan Boine
4,057 PointsDivide and conquer disemvowel or, reverse engineer it
Ok. so I didn't have any luck with this one. so I checked the community and Randy Eichelberger had a good answer.
But just copying the answer and move on doesn't do us any good, does it ? :)
So I dissected the code to understand how it works, and I think I got most of it right.
except that I don't understand why '' has to be in ''.join(split_word)
# for example disemvowel('hello')
def disemvowel(word):
vowels = ["a", "e", "i", "o", "u", "A", "E", "I", "O", "U"]
splits (word) into a list: ['h', 'e', 'l', 'l', 'o']
split_word = list(word)
split_word.remove(vowels[0]) will remove first index of vowels i.e. 'a' from the word list.
while vowels:
try:
split_word.remove(vowels[0])
when the list doesn't contain anything from vowels, it will delete it from vowels. i.e. delete 'a' and move on to the next index.
except ValueError:
del vowels[0]
this I don't fully understand. I guess when vowels is empty, it will no longer try, so it will join the list i.e. ['h', 'l', 'l',] = 'hll'
word = ''.join(split_word)
return word
whole code:
def disemvowel(word):
vowels = ["a", "e", "i", "o", "u", "A", "E", "I", "O", "U"]
split_word = list(word)
while vowels:
try:
split_word.remove(vowels[0])
except ValueError:
del vowels[0]
word = ''.join(split_word)
return word
Wish there were more hints in these task.
3 Answers
Alex Koumparos
Python Development Techdegree Student 36,887 PointsHi Tor,
Depends on what you mean by 'the next line', if you mean the next line after the whole try/except block, then yes.
It's slightly more complicated, depending on if you have else
or finally
branches in your try block, but in the typical example where you just have a try and one or more excepts, then it works as you describe. If you're interested in how else
and finally
figure into the mix, check out PEP 341 which explains every permutation of try/except/else/finally in great detail.
Cheers
Alex
Alex Koumparos
Python Development Techdegree Student 36,887 PointsHi Tor,
join() is a method on a string. If you are wanting to connect all the characters in your list with no separators, it seems kind of weird to be calling a method on an empty string. But maybe it makes more sense in the situation where you want to combine all the strings in a list together but separated with a comma and space. So given a list like: ['this', 'is', 'a', 'list', 'of', 'words'], and you wanted to get 'this, is, a, list, of, words' you would call the join method on
", "`
Hope that helps
Alex
Tor Ivan Boine
4,057 PointsBtw, forgot to ask. Using the try function, when there is nothing more to try, it automatically goes to the next line of the code, right? Is that how try works?