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 Basics All Together Now Handle Exceptions

help needed with try block

not sure if this question is valid or makes sense but maybe someone can help me, when assigning the value of num_tickets, why not assign = int(input("how many tickets would you like? ")) as we are expecting input of a number?

then following into the try block, i am struggling with the reasoning behind num_tickets = int(num_tickets). is it so the script only proceeds if the input is an int? as a str causes a ValueError?

not sure if I'm waffling here but needed help haha

Marina Potuzhnaya
Marina Potuzhnaya
2,206 Points

Hi Aaron,
I had the same idea as you and my solution excerpt is below. Thus there is no need for the num_tickets = int(num_tickets) line as we are assigning the input string and coercing it into an integer in one step rather than first assigning the string to a variable and then coercing it in the next step.

while tickets_remaining > 0:
    print("The remaining number of tickets is {}".format(tickets_remaining))
    name = input("What is your name?  ")
    try:
        tickets_required = int(input("Hi {},\nHow many tickets would you like to purchase?  ".format(name)))

3 Answers

Megan Amendola
seal-mask
STAFF
.a{fill-rule:evenodd;}techdegree seal-36
Megan Amendola
Treehouse Teacher

You're right, try blocks try to accomplish some code and if it doesn't work, then it throws an error. This is so you can catch errors so your program doesn't crash and so you can inform the programmer or user of the error so they can possibly fix their mistake.

# the code I want the program to attempt to do
try:
    # try to convert num_tickets into an integer
    # numbers will work - strings, symbols, etc. will not
    num_tickets = int(num_tickets)
# if it fails, then this except will run
except ValueError:
    # it failed and I send a message to the console
    print('you must enter a number')
# if it passes, then this else will run
else:
    # it passed so I can let the user know they bought tickets
    print(f'you bought {num_tickets}')

You could put the convert the input and put that all under the try block if you wanted like this:

# the code I want the program to attempt to do
try:
    # try to convert num_tickets into an integer
    # numbers will work - strings, symbols, etc. will not
    num_tickets = int(input('How many tickets do you want to buy? '))
# if it fails, then this except will run
except ValueError:
    # it failed and I send a message to the console
    print('you must enter a number')
# if it passes, then this else will run
else:
    # it passed so I can let the user know they bought tickets
    print(f'you bought {num_tickets}')

I think it was broken up to help breakdown what is happening.

Chris Freeman
MOD
Chris Freeman
Treehouse Moderator 67,636 Points

Good question! Wrapping the input() with an int() and placing the combined statement within the try block would also work. The advantage of using two separate statements makes the intent of the try statement focus being specifically on the int() conversion very clear.

Post back if you need more help. Good luck!!!

thanks everyone! strangely this has made my day