Tag Archives: The development of

Solving Python error: local variable ‘a’ referenced before assignment

First, the cause of the problem
Defines a variable outside a function, and then use the following variables inside a function in python, and change its value, the result error local variable ‘a’ referenced before the assignment, the code is as follows:

error reason: the python functions and global variables with the same, if you have to modify the value of the variable will become a local variable, the reference to the variable nature can appear not to define such a mistake.
Second, the solution
The problem is solved by declaring a as a global variable with the global keyword: