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

Adam Franklin
seal-mask
.a{fill-rule:evenodd;}techdegree
Adam Franklin
Python Development Techdegree Student 2,096 Points

Why won't my ValueError is the tickets are greater than the remaining tickets run?

My code seems to just jump to the except ValueError rule instead of executing my raise ValueError if num_tickets > tickets_remaining.

Any help would be appreciated.

while tickets_remaining >= 1: users_name = input("Hi! What is your name? ") num_tickets = input("Hi {}! How many tickets would you like? ".format(users_name)) #expect a ValueError to happen and handle it. try: num_tickets = int(num_tickets) if num_tickets > tickets_remaining: raise ValueError("There are only {} tickets remaining".format(tickets_remaining)) #raise a ValueError if the request is for more tickets than are available except ValueError as err: print("Oh no, we ran into an error! Please try again".format(err )) else: total_price = (num_tickets * TICKET_PRICE) print("Adam, your total will be ${} ".format(total_price)) confirm = input("Would you like to proceed? Y/N ") if confirm.upper() == "Y": print("Sold!!! ")

Can you format your code with markdown or post it as a workspace snapshot?

1 Answer

It does run. But there is no placeholder in the following string for the error message

"Oh no, we ran into an error! Please try again".format(err )

Replace that with this and you will see the message

"Oh no, we ran into an error! {}. Please try again".format(err )
Steven Parker
Steven Parker
231,248 Points

I also noticed that tickets_remaining is not being reduced on a successful sale, so only a single sale larger than the starting total would raise an error.

And as long as you purchase smaller amounts, there's an infinite supply of tickets! :wink: