Python Python Basics All Together Now Handle Exceptions

Shawna Duvall
seal-mask
.a{fill-rule:evenodd;}techdegree
Shawna Duvall
Python Development Techdegree Student 1,434 Points

After raise for quantity, not a number exception prints computer error message.

I got to the end after the part where we raise an exception for the quantity being more than the tickets_remaining and get the proper message when it fires off. However, I decided to go and check the original "please enter a number" error and now that one fires off this message.

Oh no, we ran into an issue. invalid literal for int() with base 10: 'ggg'. Please try again.

How do I keep the original error message and have the second one called with the raise"?

TICKET_PRICE = 10

tickets_remaining = 100  

while tickets_remaining:  
  print("There are {} tickets remaining.".format(tickets_remaining))
  name = input("What is your name?  ")
  print("Welcome, {}! There are {} tickets remaining".format(name, tickets_remaining))
  quantity = input("How many tickets would you like?  ")
  try:
    quantity = int(quantity)
    if quantity > tickets_remaining:
      raise ValueError("There are only {} tickets remaining.".format(tickets_remaining))
  except ValueError as err: 
    print("Oh no, we ran into an issue. {}. Please try again.".format(err))
  else:
    total = quantity * TICKET_PRICE
    print("For {} tickets the total price is ${}".format(quantity, total))
    confirm = input("Would you like to continue. Enter Y/N  ")
    if input == "Y" or "y":
      print("SOLD!")
      tickets_remaining -= quantity
    else: 
      print("We are sorry to hear that, {}! Maybe next time.".format(name))
print("Sorry, we are sold out! :(")
Heidi Fryzell
seal-mask
.a{fill-rule:evenodd;}techdegree seal-36
Heidi Fryzell
Front End Web Development Treehouse Moderator 21,233 Points

Thanks for asking this question Shanwa... I noticed the same thing. I guess if we never went back and checked entering a non-number, we never would have known. I tried to use some "if" statements to print two different errors based on if they did not enter an integer or if they entered too many tickets but I could not get it to work. The answer from Boi below makes sense but I would not have known how to do that from what we learned in this course. šŸ¤·šŸ˜€

2 Answers

The error you're getting (invalid literal for int() with base 10: 'ggg'.) is completely normal, it is the standard error handling in Python. Whenever you try to perform an int type operation on something that is other than an int type, you get this error.

If you want to preserve your original text error message, there is another way to do it. šŸ‘‡

TICKET_PRICE = 10

tickets_remaining = 100  

while tickets_remaining:
    print("There are {} tickets remaining.".format(tickets_remaining))
    name = input("What is your name?  ")
    print("Welcome, {}! There are {} tickets remaining".format(name, tickets_remaining))
    quantity = input("How many tickets would you like?  ")
    try:
        quantity = int(quantity)
        if quantity > tickets_remaining:
            raise ValueError  šŸ‘ˆ # Made change here 
    except ValueError as err:
        if "invalid" in str(err):  šŸ‘ˆ # Made change here 
            print("Oh no, we ran into an issue. Please try again.")  šŸ‘ˆ # Made change here 
        else:  šŸ‘ˆ # added else here 
            print("There are only {} tickets remaining.".format(tickets_remaining)) šŸ‘ˆ # added error msg here

    else:

        total = quantity * TICKET_PRICE
        print("For {} tickets the total price is ${}".format(quantity, total))
        confirm = input("Would you like to continue. Enter Y/N  ")
        if input == "Y" or "y":
            print("SOLD!")
            tickets_remaining -= quantity
        else: 
            print("We are sorry to hear that, {}! Maybe next time.".format(name))
print("Sorry, we are sold out! :(")

Your code also has some additional errors which are not being caught, try entering 0 in "How many tickets would you like?"

For future code comments please use proper code format, to keep the original color format, indentation, and better help to solve your problems.

Robs K
Robs K
7,146 Points
  1. *I don't think it is necessary to be redundant *

    Why print("We have {number of tickets remaining}") and when the user logs in again you print the same message?

    1. Your conditional to check for 'Y' or 'N' doesn't use the confirm variable for validation and there is no option for a user who inputs No.

Try this

ticket_price = 10
tickets_remaining = 100

while(tickets_remaining):
  try:
    name = input('What\'s your name? ')
    print(f"Welcome {name}. There are {tickets_remaining} tickets remaining.") #used python 3.5+ string formating
    quantity = int(input('How many tickets do you want? '))


    if quantity > tickets_remaining:
      print(f"There are only {tickets_remaining} tickets remaining.") # changed this line
    else:
      total = quantity * ticket_price
      print(f"For {quantity} tickets the total price is ${total}")
      confirm = input("Would you like to continue. Enter Y/N  ")
      if confirm == "Y": #if confirm
        print("SOLD!")
        tickets_remaining -= quantity
      if confirm == 'N': # if !confirm
       print(f"We are sorry to hear that, {name}! Maybe next time.")
  except:
    print("Sorry, we are sold out! :(")
    break # if we do not have any more tickets, the loop stops