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

Hai Phan
Hai Phan
2,442 Points

What's wrong with my code?

When I enter 1000 for the number of tickets, it's work, but when I enter something like "blue", it returns this "We got an issue hereinvalid literal for int() with base 10: 'blue', please try again!"
Here is my code:

TICKET_PRICE = 10

tickets_remaining = 100  

# Run this code until all tickets were sold
while tickets_remaining >0:

    print("Welcome to the shop")    

    # Promt user how many tickets remaining
    print("There are {} tickets remaining".format(tickets_remaining))

    # Get the customer name
    name = input("Please tell me what is your name?  ")

    # Get the number of ticket
    print("Hello {}".format(name))
        #Expect the ValueError to happen and handle it approriately
    try:
        tickets_requested = int(input("How many tickets would you like?  "))

        # Print this out when we dont have enough tickets to sold   
        if tickets_remaining < tickets_requested:
            raise ValueError("(There's only {} tickets remaining!)".format(tickets_remaining))
    except ValueError as err:
        print("We got an issue here{}, please try again!".format(err))
    else:
        # Calculate the price
        total_price = (tickets_requested * TICKET_PRICE)

        print("Your total ptice is ${}".format(total_price))

        # Promt the user if they want to proceed
        proceed = input("Do you want to proceed? (Y/N)  ")

        # If they want to proceed
        if proceed.lower() == "y":
            print("SOLD {} tickets!".format(tickets_requested))
            tickets_remaining -= tickets_requested
            print("There are now {} tickets remaining".format(tickets_remaining))
        else:
            print("Have a nice day {}! \nThank you for using our shop".format(name))
            print("Next customer please")

# Notify user if all tthe tickets are sold out
print("Sorry, all the tickets were sold! \nSee you next time")

1 Answer

Steven Parker
Steven Parker
231,172 Points

When a conversion error occurs, your code catches the exception and does this:

        print("We got an issue here{}, please try again!".format(err))

So you're seeing your message, combined with the normal error message generated by the exception (in "err").

You cam make it output anything you want. What would you like to get instead?

Hai Phan
Hai Phan
2,442 Points

I see there are 2 "kind" of ValueError here, one when the tickets_requested is greater than tickets_remaining, another is when I input letter. I want to get only "We got an issue here, please try again!" when I enter word, but both of these 2 are ValueError so I get a little confuse. Can you help me to solve this?

Steven Parker
Steven Parker
231,172 Points

You still did not say what message you would like to see, but here's an example of providing a different one:

    except ValueError as err:
        if (str(err).startswith("invalid literal")):
            err = " (you did not enter a number)"
        print("We got an issue here{}, please try again!".format(err))

You can make the message be anything you'd like.

Hai Phan
Hai Phan
2,442 Points

Thank you Steven, that what I really want to output, create another explanation message instead of the ordinary one. Excuse me for not saying clearly what I want to get, anyway, you still got it! Such a lot of thing to learn here.