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 trialaaron roberts
2,004 Pointshelp 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
3 Answers
Megan Amendola
Treehouse TeacherYou'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
Treehouse Moderator 68,441 PointsGood 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!!!
aaron roberts
2,004 Pointsthanks everyone! strangely this has made my day
Marina Potuzhnaya
2,206 PointsMarina Potuzhnaya
2,206 PointsHi 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.