Firstly, is that your entire program? It looks to me like there's a bit missing - after the dict_item()
function, there's an elif
which doesn't seem to match any if
I can see.
Anyway, about variables. It looks like your track
variable is at the top level of the script - this makes it a global variable. For the code at the top level, you can use it however you like. This includes inside if...else
, because these aren't functions - they'd usually be referred to as "blocks".
Inside a proper function (i.e. starts with def
), you can READ the value of a global variable. However, if you want to SET the value (i.e. track = x
) then you need to put global track
at the start of your function, like you've said there. If you don't do this, Python will assume that it's a local variable which is only available inside that call to the function, and is automatically deleted once the function exits. Next time you enter the function, local variables are re-created again from scratch.
A function can also take parameters which are put inside the brackets of the function name. If you do this then you need to call the function with these parameters, and what this does is take a copy of the value and essentially make it the same as a local variable inside the function. This means that if the function changes the value, it won't change the original (but with a global it does change). I should mention here that types like lists ([1, 2, 3]
) and dictionaries ({"one": 1, "two": 2}
) are passed as "references" which means that functions can change them. Don't worry about this too much, but just remember it for the future.
A quick example:
my_global = 1
def my_function(my_parameter):
global my_global
my_global = my_global + 1
my_local = 1
my_local = my_local + 5
my_parameter = my_parameter + 10
print "Parameter: %d" % (my_parameter,)
print "Local: %d" % (my_local,)
print "Global: %d" % (my_global,)
my_function(123)
my_function(my_global)
my_global = 100
my_function(123)
This produces the following output:
Parameter: 133
Local: 6
Global: 2
Parameter: 12
Local: 6
Global: 3
Parameter: 133
Local: 6
Global: 101
So, the first time the function is called, the parameter is 123
, which is 133
when you add 10
. The global starts off as 1
, the function adds 1
to it to make 2
. The local is always 6
because it's re-created every time the function is called - it starts as 1
and the function then adds 5
to make 6
.
Once the function has ended the first time, the value of my_global
is 2
because the that's what happens with globals - if the function changes them, they stay changed.
So the second time the function is called, the parameter is actually the value of my_global
but because we pass a copy then when the function adds 10
to it, we actually only change the copy that the function has, so the parameter is 12
(because my_global
was 2
after the previous call to the function and the function adds 10
to that). The actual value of my_global
after the function is 3
, because it adds 1
to it again.
Now the main script assigns 100
to my_global
, replacing any previous value. So this time when the function adds 1
it gets 101
.
Hopefully that clears up some confusion about global variables. The important thing is that if you forget the global
line at the start of the function, you might end up with a local variable which has the same name and it "covers up" the global one inside the function. Or you might just get an error, like you do with this script.
However, I don't actually think that's the cause of your problem - I still wanted to explain that because it's really important to understand how variables and functions work in Python. I think your problem is because each time you make a new HTTP request, your script is entirely run again from scratch - so every single variable (including globals!) is reset back to its initial value. If you want to store information between one page and another page, you need to use the database to do that (or make a file on disk or something like that).
This isn't always true in web frameworks, but definitely for CGI scripts your entire program is re-run every time you ask for a new page in the browser - nothing is saved, except what you put in the database. Even in frameworks when your program isn't re-run, you always put things you want to save in a database or in a file on disk, because you don't want to lose anything when your program is restarted.
Does that help?