I know it’s a heavily discussed topic, I know there are tons of threads out there about it (I’ve read them all), but I still Just Don’t Get It.
Undefined variables (both local and global) are my biggest problem in Python and I have no idea what I’m doing when trying to fix them. I’ll go through and add an x = 0 or a var = func(var) some where and some times it works and sometimes it doesn’t. It’s incredibly frustrating and I really need some help. I’ve asked my teacher but it usually takes her a while to get back to me and I’d like to get this thing fixed.
Here’s what I’m working on: http://pastebin.com/KTuh7D2p (This is for school.)
Can someone please explain variable thing to me?
I keep getting errors like this:
Traceback (most recent call last):
File "Project1-v2.py", line 144, in <module>
main()
File "Project1-v2.py", line 12, in main
itemsSave = tagYell(items, itemsSale, itemsSave)
UnboundLocalError: local variable 'itemsSave' referenced before assignment
In this particular case, I would guess that it is because you are trying to use
itemSaveto define itself. This is equivalent to saying something like:the very first time you define
x.You need to give some context as to what
itemSaveis before you can try and use its value.For example, this program:
Would give me an error message of:
This however, would work fine, and print “15”: