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

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

Eric Peppler
Eric Peppler
3,633 Points

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')