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

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

TICKET_PRICE = 10

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))
  try:
      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))
  else:
      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
        print("SOLD!!")
        tickets_remaining -= num_tickets
      else:
        print("Thank you anyways, {}!".format(user_name))    
print("Sorry, tickets are all sold out!")

2 Answers

Chris Freeman
MOD
Chris Freeman
Treehouse Moderator 68,441 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!!!

boi
boi
14,242 Points

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 68,441 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.

boi
boi
14,242 Points

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 68,441 Points

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

Chris Freeman
Chris Freeman
Treehouse Moderator 68,441 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’>
boi
boi
14,242 Points

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: