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 trialSabeen Azhar
440 Pointswhy is there a problem with except
what is wrong with the except block mid way through the script?
import sys
TICKET_PRICE = 10
tickets_remaining = 100
def calculate_price(TICKET_PRICE, number_of_tickets)
while tickets_remaining > 1:
print("there are {} tickets left".format(tickets_remaining))
name = input("what is your name? ")
number_of_tickets = int(input("how much tickets would you like, {}? ".format(name)))
try:
except ValueError:
print("oh no we ran into an issue. try again")
else:
amount_due = calculate_price(number_of_tickets)
print("that would be ${}, {}.".format(amount_due, name))
print("are you sure you want to go through with this? ")
confirm = input("do you want to proced? Y/N ")
while confirm.lower() not in ("y", "n"):
print("whoops! try again!")
confirm = input("do you want to proced? Y/N ")
if confirm == "y":
print("SOLD!")
tickets_remaining -= number_of_tickets
elif confirm == "n":
sys.exit("thanks anyways {}!".format(name))
print("out of tickets sorry!")
thanks in advance!
2 Answers
jb30
44,806 PointsInstead of
number_of_tickets = int(input("how much tickets would you like, {}? ".format(name)))
try:
except ValueError:
try putting a line of code between try and except, such as
try:
number_of_tickets = int(input("how much tickets would you like, {}? ".format(name)))
except ValueError:
If there isn't anything in the try block, there isn't anything that the except block can catch.
Sabeen Azhar
440 PointsThank you for the answer! on an unrelated note, how do add colour to the code segments?
jb30
44,806 PointsYou can specify the language (python) right after the first three backticks.