Unboundlocalerror: local variable ‘var’ referenced before assignment
local variable “var” referenced before assignment
error example 1
var = 10
def f1():
print(var)
var = 2
print(var)
f1()
When VaR is a global variable, it is modified and undeclared in the function
solution:
declare the global variable Global var before calling
error example 2
def f2():
var = 0
def f3():
var *= 0
return var
return f3()
f2()
VaR is a local variable defined in F2. Its value is modified in nested functions and is not declared
solution:
declare nonlocal var in nested functions in advance
Read More:
- [Solved] supervisor Error: /usr/local/lib/python2.7/dist-packages/pkg_resources/py2_warn.py:22: UserWarning: Setuptools will stop working on Python 2
- Python: SVN deletes files on local and remote repositories
- How to Fix Python reading large local file memory error
- MAC OS install python3 Error: Permission denied @ dir_s_mkdir-/usr/local/Frameworks
- [Solved] weditor Plug-in Dump Hierarchy Error: Local server not started, start with $ python -m weditor
- [Solved] scikit-learn Error: ModuleNotFoundError: No module named ‘sklearn.utils.linear_assignment_’
- Tensorflow: Common Usage of tf.get_variable_scope()
- RuntimeError: Invalid DISPLAY variable [How to Solve]
- [Solved] Mujoco Error: Missing path to your environment variable.
- [Solved] NPM install Error: check python checking for Python executable python2 in the PATH
- [Solved] RuntimeError: a view of a leaf Variable that requires grad is being used in an in-place operation
- [Solved] yolo Error: IndexError: invalid index to scalar variable.
- [Solved] RuntimeError: a view of a leaf Variable that requires grad is being used in an in-place
- Invalid python sd, Fatal Python error: init_fs_encoding: failed to get the Python cod [How to Solve]
- How to Solve Python WARNING: Ignoring invalid distribution -ip (e:\python\python_dowmload\lib\site-packages)
- [Solved] opencv-python: recipe for target ‘modules/python3/CMakeFiles/opencv_python3.dir/all‘ failed
- [Solved] cv2.error: OpenCV(4.6.0) D:\a\opencv-python\opencv-python\opencv\modules\……
- Linux installs Python and upgrades Python
- [Solved] PyCharm Error: Cannot open Local Terminal Failed to start [powershell.exe] in C:\Users\…….
- [Solved] Can‘t pickle local object ‘_createenviron.<locals>.encodekey‘