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

James Kelley
James Kelley
276 Points

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 27,653 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
218,639 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 of your workspace.

3 Answers

victor escarcega
victor escarcega
15,083 Points

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

James Kelley
James Kelley
276 Points

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 27,653 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!