Welcome to the Treehouse Community

The Treehouse Community is a meeting place for developers, designers, and programmers of all backgrounds and skill levels to get support. Collaborate here on code errors or bugs that you need feedback on, or asking for an extra set of eyes on your latest project. Join thousands of Treehouse students and alumni in the community today. (Note: Only Treehouse students can comment or ask questions, but non-students are welcome to browse our conversations.)

Looking to learn something new?

Treehouse offers a seven day free trial for new students. Get access to thousands of hours of content and a supportive community. Start your free trial today.

Python Python Basics All Together Now Handle Exceptions

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

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")

I have the same issue, but my code is slightly different. Here is my code bellow:

tickets_remaining = 100
while tickets_remaining >=1: print("There are {} tickets remaining.".format(tickets_remaining)) name = input("What is your name? ") num_tickets = input("How many tickets would you like, {}? ".format(name)) # Expect a ValueError to happen and handle it appropriately...remember to test it out! try: #raise ValueError:

  num_tickets = int(num_tickets)
  # Raise a ValueError if the request is for more tickets than are avaible
  if num_tickets > tickets_remaining:
      raise ValueError("There are only {} tickets remaining".format(tickets_remaining))

except ValueError as Err: # Include the error text in the output print("Oh no, we ran into an issue. {}. Please try again".format(Err)) else: amount_due = num_tickets * TICKET_PRICE print("The total due is ${}".format(amount_due)) should_proceed = input("Do you want to proceed? Y/N ") if should_proceed.lower() == "y": # TODO": Gather credit card information and process it. print("SOLD") tickets_remaining -= num_tickets else: print ("Thank you anyways, {}!".format(name)) print("Sorry the tickets are all sold out!!!")

3 Answers

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":

Still does not reduce number of remaining tickets

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.