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 Handle Exceptions

Trouble when using "except ValueError as err:" Console displays "invalid literal for int() with base 10: 'blue'."

Near the end of the video, the lecturer has us use an "as err". I would appreciate help on where I went wrong.


tickets_remaining = 100  

while tickets_remaining >=1:
    name = input("What is your name?   ")

    print("There are {} tickets remaining, {}".format(tickets_remaining, name))

    tickets_wanted = (input("How many tickets would you like to purchase?  "))


        tickets_wanted = int(tickets_wanted)

        if tickets_wanted > tickets_remaining:

            raise ValueError("There are only {} tickets remaining.".format(tickets_remaining))

    except ValueError as err:

        print("Oh no, we ran into an issue. {}. Please try again".format(err))


        total_price = tickets_wanted * TICKET_PRICE

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

        purchase_confirmation = input("Do you want to purchase? Y/N  ")

        tickets_remaining -= tickets_wanted

        if purchase_confirmation.lower() == "y":

            print("SOLD! {} tickets left.".format(tickets_remaining))


            print("Okay, goodbye {}. ".format(name))

print("Sorry the tickets are all sold out!!") 

2 Answers

Chris Freeman
Chris Freeman
Treehouse Moderator 67,636 Points

Hey Tsvetelin Buhlev, unfortunately, that text message is created by the Python when "blue" is presented to the int() function:

$ python
Python 3.8.5 (default, Jul 28 2020, 12:59:40) 
[GCC 9.3.0] on linux
Type "help", "copyright", "credits" or "license" for more information.
>>> int('blue')
Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
ValueError: invalid literal for int() with base 10: 'blue'

That is one of the drawbacks to using as err, you get what the system implementation gives you.

Post back if you have more questions. Good luck!!

Frederic Stein
Frederic Stein
1,505 Points

Does this mean that by implementing the as err scenario, we made the former ValueError structure obsolete?

Chris Freeman
Chris Freeman
Treehouse Moderator 67,636 Points

Frederic Stein, the same raised ValueError instance still exists, except now it has an additional label defined as “err” that can be used to reference it.

Frederic Stein
Frederic Stein
1,505 Points

So my code:


tickets_remaining = 100  

def price_calculation(num_tickets):
    return num_tickets * TICKET_PRICE + SERVICE_CHARGE

while tickets_remaining >= 1:
    print("Hi, there are {} tickets remaining.".format(tickets_remaining))
    name = input("What's your name? ")
    num_tickets = input("Hi {}, how many tickets would you like to buy? ".format(name))
        num_tickets = int(num_tickets)
        if num_tickets > tickets_remaining:
            raise ValueError("Sorry {}, we only have {} tickets left.".format(name, tickets_remaining))
    except ValueError as err:
        print("We are having a problem here. {}".format(err))
        amount_due = price_calculation(num_tickets)
        order_confirmation = input("Would you like to buy {} tickets for $ {}  (y/n)".format(num_tickets, amount_due))
        order_confirmation = order_confirmation.lower()
        #Gather credit card information
        if order_confirmation == 'y':
            print("Thank you for your purchase")
            tickets_remaining -= num_tickets
            print("Thank you {} for your visit".format(name))
    print("Sorry, we're out of tickets")

provides this failure message "We are having a problem here. invalid literal for int() with base 10: 'blue' " when confronted with the invalid "number". According to my taste "We are having a problem here." would be sufficient :)