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 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
231,269 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! :(")