Welcome to the Treehouse Community

Want to collaborate on code errors? Have bugs you need feedback on? Looking for an extra set of eyes on your latest project? Get support with fellow developers, designers, and programmers of all backgrounds and skill levels here with the Treehouse Community! While you're at it, check out some resources Treehouse students have shared here.

Looking to learn something new?

Treehouse offers a seven day free trial for new students. Get access to thousands of hours of content and join thousands of Treehouse students and alumni in the community today.

Start your free trial

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
231,248 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).

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
231,248 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")

Wow, thank you very much! I have been struggling with this issue for a few days. To think that I couldn't figure this beep out, I was about to question my existence. Thanks to your answer I know my code is ok .

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

Steven Parker
Steven Parker
231,248 Points

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

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!

@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.