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 Cleaner Code Through Refactoring

vikas Rai
vikas Rai
9,703 Points

If user enters "too many" as an input for number of tickets then i am getting "invalid literal for int() with base 10: "

When we are catching auto raised ValueError(user is putting string value for number of tickets) and manually raised ValueError(number of tickets>remaining tickets) under same same ValueError exception then there is an issue.

2 Answers

Steven Parker
Steven Parker
210,425 Points

Don't worry, that is the correct behavior.

What you are seeing is the default message generated by the system when an exception is thrown from attempting to convert a string that does not consist of digits (like "too many") into a number.

How can we tweak the code in order not to get this response? in the video after raising ValueError for entering a string. when entered, u get only the message raised by the ValueError. with no such response as "invalid literal for int() with base 10". after adding the second ValueError for number of tickets>total number of tickets, when running the code while entering a string, yields this error. how can we fix it?

Here is the code.

ticket_price = 10 tickets_remaining= 100

def calculate_price(number_of_tickets)

while tickets_remaining >= 1:

print("there are {} tickets remaining.".format(tickets_remaining))
name = input("what's your name?")
try:
    tickets_number = int(input("Hello {}, how many tickets would you like to buy?".format(name)))
    if tickets_number > tickets_remaining:
        raise ValueError("there are only {} tickets remaining.".format(tickets_remaining))
    price = ticket_price * tickets_number
    print("the total price is {}$".format(price))

except ValueError as err:
    print("Ops try again!{}".format(err))

else:
    procceed = input("would you like to procceed Y/N?")

    if procceed.lower() == "y":
        print("SOLD!")
        tickets_remaining -= tickets_number
    else: 
        print("thank you {}!".format(name))

print("Sorry, all tickets are sold out! :(")