Python Python Basics All Together Now Handle Exceptions

Ritam Saha
Ritam Saha
1,137 Points

When entering amount of tickets wanted, it loops back into the while loop making it impossible to complete the purchase

import time

TICKET_PRICE = 10

tickets_remaining = 100  
tickets_remaining = int(tickets_remaining)
while tickets_remaining > 0:
  print("There are {} tickets remaining.".format(tickets_remaining))

  name = input("What is your name?")
  tickets_requested = input("Hello {}, how many tickets would you like to buy?".format(name))
  #Expect a ValueError to happen and handle it
  try:
      tickets_requested = int(tickets_requested)
      if tickets_requested > tickets_remaining:
        raise ValueError("There are only {} tickets remaining".format(tickets_remaining))
  except ValueError as err:
      print("Oh no, Please make sure that the value you entered was a number, try again.")
  else:
    if tickets_requested > 1:
      ticket = "tickets"
    else:
      ticket = "ticket"

      total_cost = tickets_requested * TICKET_PRICE
      total_cost = str(total_cost)
      #output price to screen
      print("The total cost for {} {}, is ${}".format(tickets_requested,ticket,total_cost))

      proceed = input("Would you like to proceed? Y/N")
      if proceed.lower() == "y":
        print("SOLD! Purchase confirmed")
        tickets_remaining -= tickets_requested
      else:
          print("Thank you {} for your time. Goodbye".format(name))

print("Sorry, All the tickets are sold out.")

1 Answer

Ritam Saha
Ritam Saha
1,137 Points

I found out the issue.I had bad indention on the part where it asked if i ordered more than 1 ticket and it would only run if i had bought 1 ticket