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
spencer tintorri
6,184 PointsExcept block runs. Then what? Code above the Try block runs?
while tickets_remaining >= 1:
print("There are {} tickets remaining!".format(tickets_remaining))
name = input("Hey there, what is your name, fam? ")
# ValueError - handle if they dont input a number
try:
tickets_requested = int(input("How many tickets would you like, {}? ".format(name)))
# Raise ValueError if tickets_requested > tickets_remaining
except ValueError:
# Include the error text in the output
print("Oh no! That's not a valid value. Put in a number, dumb dumb.")
else:
total_cost = tickets_requested * TICKET_PRICE
print("The total cost of {} tickets is ${}.".format(tickets_requested, total_cost))
proceed = input("{}, would you like to complete your purchase of {} tickets? Y or N? ".format(name, tickets_requested))
In this example, answering "How many tickets would you like?" with a non-integer runs this
Oh no! That's not a valid value. Put in a number, dumb dumb.
There are 100 tickets remaining!
Hey there, what is your name, fam?
I thought the beginning of the Try block is supposed to run after the Except block runs. Or am I misunderstanding something?
1 Answer
Alex Koumparos
Python Development Techdegree Student 36,888 PointsHi Spencer,
The sequence with try/except/else is as follows:
First, attempt to execute the code inside the try block;
Next, if the try code succeeds, execute the else block (then continue executing the code after the try/except/else);
Otherwise, if the try code failed with the specified exception, execute the corresponding except block (then continue executing the code after the try/except/else);
Otherwise, if the try code failed with a non-specified exception, crash.
Since you have no code inside your while loop after the try/except/else sequence, the next line after the sequence is to go back to the first line in the while loop.
Hope that clears things up for you.
Cheers
Alex