Python Python Basics All Together Now Cleaner Code Through Refactoring

Error handling works but will not communicate the error type back to the user.

When an error is encountered the amount due does not calculate as expected but the error type is not printed so the user knows what happened. The loop just circles back to the beginning.

SRV_CHRG = 2
TCKT_PRICE = 10

tckt_left = 100

def calc_price(num_tckt):
   return(num_tckt*TCKT_PRICE)+SRV_CHRG

while tckt_left >=1:
   print("There are {} tickets remaining.".format(tckt_left))
   name=input("What is your name?")
   num_tckt = input("Hey {}. How many tickets would you like?".format(name))
   try:
      num_tckt = int(num_tckt)
      if num_tckt > tckt_left:
         raise ValueError ("There are only {} tickets left".format(tckt_left))
   except ValueError as err:
      print("Sorry, we ran into an issue. {}.  Please try again".format(err))
   else:
      amount_due = calc_price(num_tckt)
      print("The total due is ${}".format(amount_due))
      should_proceed = input("Do you want to proceed, Y/N?")   
      if should_proceed.lower() == "y":
         print("SOLD")
         tckt_left = tckt_left-num_tckt
      else:
         print("Thanks anyways {}".format(name))

print("Sorry we are sold out, {}".format(name))
Louise St. Germain
Louise St. Germain
17,948 Points

Could you post the code you are talking about? It's hard to guess what the problem might be without seeing it...

2 Answers

Sorry. Code has been added

Louise St. Germain
Louise St. Germain
17,948 Points

That's odd - I copied your code above and pasted it into a file in Workspaces, and when I run it, it's working fine. If I ask for 120 tickets, it does pop up that "Sorry, we ran into an issue..." message.

Is that not happening for you? Are you in Workspaces, or using some other editor?

I'm using a file in Workspaces. Maybe there is a flaw in my workspaces. When I put a space between the ValueError and the print function, all of a sudden it works.