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![](https://ecs-static.teamtreehouse.com/assets/views/marketing/shared/community-banner-white-47072046c51352fe6a69f5e691ff5700b28bb11d45197d7bdf066d9ea3f72d0c.webp)
![aaron roberts](https://secure.gravatar.com/avatar/df22af60f56df987eb202220f5d8bc32?s=96&d=https%3A%2F%2Fecs-static.teamtreehouse.com%2Fassets%2Fcontent%2Fdefault_avatar-445fbbabfc8dc9188fb5967fe43322ee0c3e0dd1e10f378bf8343784af5a13eb.webp&r=pg)
aaron 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](https://uploads.teamtreehouse.com/production/profile-photos/7340382/micro_headshot.jpg)
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](https://uploads.teamtreehouse.com/production/profile-photos/1153532/micro_image.jpg)
Chris Freeman
Treehouse Moderator 68,425 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](https://secure.gravatar.com/avatar/df22af60f56df987eb202220f5d8bc32?s=96&d=https%3A%2F%2Fecs-static.teamtreehouse.com%2Fassets%2Fcontent%2Fdefault_avatar-445fbbabfc8dc9188fb5967fe43322ee0c3e0dd1e10f378bf8343784af5a13eb.webp&r=pg)
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.