Welcome to the Treehouse Community

The Treehouse Community is a meeting place for developers, designers, and programmers of all backgrounds and skill levels to get support. Collaborate here on code errors or bugs that you need feedback on, or asking for an extra set of eyes on your latest project. Join thousands of Treehouse students and alumni in the community today. (Note: Only Treehouse students can comment or ask questions, but non-students are welcome to browse our conversations.)

Looking to learn something new?

Treehouse offers a seven day free trial for new students. Get access to thousands of hours of content and a supportive community. Start your free trial today.

Python Python Collections (Retired) Lists Redux Disemvoweled

Jeff Williams
Jeff Williams
1,111 Points

What 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
Matthew Hill
7,799 Points

The '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.