Python Python Basics All Together Now Handle Exceptions

ERDAL DINCER
ERDAL DINCER
1,619 Points

I repeated everything, however I couldnot reduce number of the tickets and could not raise an error

I repeated everything, however I couldnot reduce number of the tickets and could not raise an error if the requested tickets numbers are greater than remainings

ERDAL DINCER
ERDAL DINCER
1,619 Points

ticket_price=100 tickets_remaining=100

Run this code continuosly until we run out of tickets

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

    name=input("What is your name?")
    num_tickets=input("How many tickets do you need {}?".format(name))
  #Except a valueerror 
    try : 
         num_tickets= int(num_tickets)
         if num_tickets > tickets_remaining:
            raise ValueError("there are only {} tickets remaining".format(tickets_remaining)
    except ValueErroras Err:
         print("Oh no, we run into issues.Please, try again")   
    else:

        amount_due= num_tickets * ticket_price
        print("Total amount is {}".format(amount_due))
        should_proceed=input("Do yo want to buy Y/N?")
        if should_proceed.lower()=="Y":
               print("sold")
               tickets_remaining-= num_tickets
        else:
                print("Thank you very much {}".format(name))
      #Thankyou by name

print("Tickets are all sold out")

3 Answers

KRIS NIKOLAISEN
KRIS NIKOLAISEN
54,164 Points

You have a few issues:

1) Missing closing parenthesis here:

 raise ValueError("there are only {} tickets remaining".format(tickets_remaining)

2) There should be a space between ValueError and as here:

except ValueErroras Err:

3) You should be comparing lowercase should_proceed to lowercase "Y" here ("y" instead of "Y"):

if should_proceed.lower()=="Y":
ERDAL DINCER
ERDAL DINCER
1,619 Points

Still does not reduce number of remaining tickets

KRIS NIKOLAISEN
KRIS NIKOLAISEN
54,164 Points

Here is a snapshot of your code with those changes. Note: I also put a space between tickets_remaining and -= but tested with and without and it didn't matter. To run enter python a.py in the console.