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 trial

Python

Why do I need to have the coercion of the num_tickets in the try block immediately after it is declared?

In the video Craig makes the variable num_tickets, after which, it is coerced into an integer value. Upon declaring the try block code to accept the 'ValueError', the coercion ( num_tickets = int(num_tickets) is placed immediately after the try block is declared, but before the except ValueError portion of the code.

Why is this done?

How does the computer process this information?

1 Answer

Steven Parker
Steven Parker
243,656 Points

I think the whole point of the "try" is to catch errors that might occur during that type conversion. So that statement that performs the conversion has to be in the "try block", which is the indented lines between the "try" and the "except".