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 trialJeff Williams
1,111 PointsWhat do 'try' and 'except' do?
What is the importance of having the 'try:' on line 10 and the 'except:' on line 12?
1 Answer
Matthew Hill
7,799 PointsThe 'while True:' part of the program ensures that it will keep looping through the list and removing each vowel, however, this will continue forever as True will always be True. Eventually all the vowels will have been removed. When the program tries to list.remove('a') and there is no 'a' in the list, it will raise an exception, in this case a ValueError.
A try/remove block will do the try part unless it finds an exception. This means that the program will keep looping round and .remove()ing all the vowels until there are none left, once there are none left a ValueError will be raised and this will take it to the except block which contains a break, the break gets you out of the infinite 'while True:' loop.
Jeff Williams
1,111 PointsJeff Williams
1,111 PointsOk thanks a lot!