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:
Read More:
- Python error: local variable ‘XXXX’ referenced before assignment
- Python UnboundLocalError: local variable ‘num’ referenced before assignment
- Global variable error: unboundlocalerror: local variable ‘l’ referenced before assignment
- Differences between shell script variable assignment and C language variable assignment
- Can’t find Python executable “D:\python3\python.exe”, you can set the PYTHON env variable.
- non-static variable this cannot be referenced from a static context
- Python Numpy.ndarray ValueError:assignment destination is read-only
- Error: global variable is ambiguous (conflict between using namespace STD and global variable)
- Python error type error: ‘range’ object does not support item assignment, solution
- Python’s direct method for solving linear equations (5) — square root method for solving linear equations
- Python global variables and global keywords
- Error in installing Matplotlib Library: permissionerror: [errno 13] permission denied: ‘/ usr / local / lib / python3.6’
- [leetcode] 926. Flip string to monotone increasing problem solving report (Python)
- Cannot remove entries from nonexistent file /usr/local/bin/anaconda2/lib/python2.7/site-packages/eas
- Error: transfer of control bypasses initialization of: variable XXX solution
- Arrow function should not return assignment no-return-assign
- Using the decorator may prompt property assignment expected. Vetur (1136)
- Assignment under multiple single edges is not supported for synthesis
- Error lnk1120: 2 unresolved external commands: vtkrenderingopengl_ Autoinit (construct, destroy), which is referenced in
- Node configuration environment variable and global installation of webapck