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 Branch and Loop

NameError: name "tickets_remaining" is not defined

The portion where I decrement the tickets_remaining keeps returning the above-mentioned error message. I used the placeholder -= shown in the video. I don't know why it keeps returning the error message.

https://w.trhou.se/10qytzrjs7

Jeff Muday
Jeff Muday
Treehouse Moderator 28,720 Points

James,

We'd have to see your code to make a determination. You can fork a workspace or put your code as a block of an email and we could comment on it.

Usually, a variable name is mistyped, out of scope, or possibly, uninitialized before it is used. Check to see if tickets_remaining is given an initial value in your problem.

If it isn't paste it into the forum.

best regards!

Steven Parker
Steven Parker
231,269 Points

For help with Jeff's suggestions about sharing code, you might want to take a look at this video about Posting a Question, and perhaps also this one about making a snapshot (note: not a "screenshot") of your workspace.

4 Answers

victor E
victor E
19,146 Points

very likely you did not declare your variable at the beginning before adding or subtracting to it.

Okay, I edited my original post and added a link to a screenshot of my workspace. Should I have forked the screenshot?

Jeff Muday
MOD
Jeff Muday
Treehouse Moderator 28,720 Points

That is a great way to get comments on your code, it is much appreciated!

You almost had it! All we have to do is change 1 line, but that was it. We can definitely build on that.

There are so many ways this can be fixed, but the easiest is to replace tickets_left with tickets_remaining. Specifically, the line

tickets_left -= tickets_left

should be

tickets_remaining -= tickets_remaining

was causing your issue. tickets_left was used before it was initialized.

Your program is working after that.

The next step in writing the program is to put in a looping structure and some conditional logic that will keep your ticket sales from accepting orders for more tickets than are for sale. But I'll leave that fun for you!

Enjoy your Python journey!

taliarosen
taliarosen
2,492 Points

I think the code you're looking for is tickets_remaining -= tickets_purchased