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

I keep getting a NameError that says "name 'err' is not definied", but i'm pretty sure my code is the same as in the vid


tickets_remaining = 100  

while tickets_remaining > 0: 
  print("There are {} tickets remaining.".format(tickets_remaining))
  user_name = input("Welcome to Master Ticket! What is your name?      ")
  num_tickets = input("How many tickets would you like, {}?     ".format(user_name))
      num_tickets = int(num_tickets)
      if num_tickets > tickets_remaining:
          raise ValueError("There are only {} tickets remaining".format(tickets_remaining))
  except ValueError:
      print("Oh no, we ran into an issue. {}. Please try again".format(err))
      amount_due = TICKET_PRICE * num_tickets
      print("Your total due is {}".format(amount_due))
      proceed = input("Do you want to proceed, {}?  Y/N     ".format(user_name))
      if proceed == "Y":
        # TODO: gather credit card information and process it
        tickets_remaining -= num_tickets
        print("Thank you anyways, {}!".format(user_name))    
print("Sorry, tickets are all sold out!")

2 Answers

Chris Freeman
Chris Freeman
Treehouse Moderator 67,893 Points

Hey Skye Chao, you are very close.

The line is

except ValueError as err:

The as allows referring to the error object as err

Post back if you need more help. Good luck!!!

It seems we posted the answer at the exact moment in the universe TimeLine.

why do we need to refer to ValueError as err? Is it so that it's more userfriendly when it's printed as text

Chris Freeman
Chris Freeman
Treehouse Moderator 67,893 Points

Skye Chao, the except statement looks for a specific error type but does not automatically give access to the error object raised. Using as gains a reference to the error object that otherwise doesn’t have a variable name to access. “err” is just a label. It could be “e”, “error”, etc.

This has happened at least 3 times now. When I try to respond to a problem, you appear like a few moments before me. This battle is NOT over!!! I will make a comeback someday.

Chris Freeman
Chris Freeman
Treehouse Moderator 67,893 Points

boi, until then, sir, may your posts be swift and your code sublime!

Chris Freeman
Chris Freeman
Treehouse Moderator 67,893 Points

Skye Chao, the error object is deleted once the except block finishes. But one may assign it an additional label to keep it around for the curious:

>>> # cause a ValueError
>>> try:
...     int("$")
... except ValueError as e:
...     keep_e = e
>>> # examine the error object
>>> keep_e
ValueError(invalid literal for int() with base 10: ‘$’”,)
>>> print(keep_e.__traceback__)
<traceback object at 0x111342108>
>>> import traceback
>>> traceback.print_tb(keep_e.__traceback__)
  File <stdin>, line 2, in <module>
>>> type(keep_e)
<class ValueError>

The error is pointing you exactly where the error should be, "name 'err' is not defined".

"err" has no value set to it. Craig sets except ValueError as err, so it should be like:

except ValueError as err: