The following script produces a “local variable ‘var’ referenced before assignment” error:
def func1():
var = var + 1
var = 1
func1()
How would you suggest to correct the code? Why does this error occur when the variable is declared in the script before the function is called?
You can use a global variable in other functions by declaring it as global in each function that modifies it:
After OP edited question:
If you are moving the variable after you define the function you will need to pass it to the function as a parameter.