Python Python Basics All Together Now Handle Exceptions

Getting a weird message in my error response

Code

TICKET_PRICE = 10

tickets_remaining = 100  

while tickets_remaining >= 1:
    print("There are {} remaining".format(tickets_remaining))
    gina = input("What's your name? ")  
    number_tickets = input(" How many tickets would you like {}? ".format(gina))
    try:
      number_tickets = int(number_tickets)
      if number_tickets > tickets_remaining:
          raise ValueError("There are only {} tickets remaining".format(tickets_remaining))
    except ValueError as err:
      print("Oh no, i'm not intelligent! {}. ".format(err))   
    else:
      price_of_tickets = number_tickets * tickets_remaining                      
      print("The tickets will cost {}".format(price_of_tickets)) 
      proceed = input(" Do you want to proceed? Enter Y/N")
      if proceed.lower() == "y":
          print(" SOLD")
          tickets_remaining -= number_tickets
      else:
        print("Thank you {}".format(gina))

print("sold out")

Response from terminal 
What's your name? Monty                                                                                                   
 How many tickets would you like Monty? Python                                                                            
Oh no, i'm not intelligent! invalid literal for int() with base 10: 'Python'.

3 Answers

Steven Parker
Steven Parker
201,997 Points

It may look "weird" but that's actually the standard system error raised when you perform an "int" operation on something that is not a number! It's working correctly.

And when posting code to the forum, use Markdown formatting to preserve the appearance (including indentation, which is essential for Python).

Jon Koch
Jon Koch
1,448 Points

Steven, you mention in another comment we can replace the message with something more friendly. I'm having the hardest time figuring where to put this. Honestly, I don't even want to admit how long I've been fighting with it. What would a person do to change that nasty bit of error to something friendly with the ValueError exception already raised?

Steven Parker
Steven Parker
201,997 Points

To get a more a "friendly" message than the built-in error text, you could check if the exception string contains the standard text and issue your own instead:

    except ValueError as err:
        # check for the "unfriendly" standard message
        if "base 10" in str(err):
            # then do NOT include the standard text in output
            print("Oh no! That's not a valid entry! You must type a number. Do try again...")
        else:
            # Include the error text in the output
            print(f"Oh no, we ran into an issue. {err}. Please try again")

Is there a way of translating that message into a human language for the end user?

Steven Parker
Steven Parker
201,997 Points

You could certainly have some code replace that message with a more "friendly" one.

Nikolai Olekhnovitch
Nikolai Olekhnovitch
5,278 Points

Is there any reason why Craig's code doesn't get the built-in "base 10" error text? My code is the same as his, but I do get that default base 10 error text. Thank you!

okilamb944
okilamb944
1,583 Points

@Nikolai, In Craig's demo he does not test inputting a string. We don't see the error in Craig's console because he is only testing the exception when users enter an integer greater than the tickets remaining. If he ran "blue" again for number of tickets, we would have seen the same error shown in this original post.