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 trialSkye Chao
Python Development Techdegree Student 589 PointsI 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
Treehouse Moderator 68,457 PointsHey 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
14,242 PointsThe 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:
boi
14,242 Pointsboi
14,242 PointsIt seems we posted the answer at the exact moment in the universe TimeLine.
Skye Chao
Python Development Techdegree Student 589 PointsSkye Chao
Python Development Techdegree Student 589 Pointswhy 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
Treehouse Moderator 68,457 PointsChris Freeman
Treehouse Moderator 68,457 PointsI think I was 2 seconds ahead. Pretty close!
Chris Freeman
Treehouse Moderator 68,457 PointsChris Freeman
Treehouse Moderator 68,457 PointsSkye Chao, the
except
statement looks for a specific errortype
but does not automatically give access to the error object raised. Usingas
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
14,242 Pointsboi
14,242 PointsThis 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
Treehouse Moderator 68,457 PointsChris Freeman
Treehouse Moderator 68,457 Pointsboi, until then, sir, may your posts be swift and your code sublime!
Chris Freeman
Treehouse Moderator 68,457 PointsChris Freeman
Treehouse Moderator 68,457 PointsSkye 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: