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

Marcus De Silva
Marcus De Silva
1,717 Points

Help with raising exceptions.

My program works, but when it encounters an exception, it prints out the exception and the traceback error. Im not sure how to fix that.

try: tickets_purch = input("How many tickets would you like, {}? ".format(user_name.title())) tickets_purch = int(tickets_purch)
except ValueError: print('Sorry... Please enter a number') if tickets_purch > tickets_remaining: raise Exception('Sorry, only {} remaining...'.format(tickets_remaining))

else: cart = tickets_purch * TICKET_PRICE print("Your total is ${} .".format(cart))

finalize_cart = input('Do you want to proceed? Y/N ')
if finalize_cart == 'y':
  print('{}, Your order is complete!'.format(user_name))

tickets_remaining -= tickets_purch

if finalize_cart == 'n':
  print('Awwwww :(, your order is canceled')

print("Sorry, We are all sold out")

Output:

What is your name? Marcus There are 100 tickets remaining. How many tickets would you like, Marcus? 80000 Traceback (most recent call last): File "/home/treehouse/workspace/masterticket.py", line 16, in <module> raise Exception('Sorry, only {} remaining...'.format(tickets_remaining)) Exception: Sorry, only 100 remaining...

1 Answer

Try defining tickets_purch first, then coercing the input.

user_name = "Eric"
tickets_purch = input("How many tickets would you like, {}? ".format(user_name))
try:
    tickets_purch = int(tickets_purch)
except ValueError:
    print('Sorry... Please enter a number')