Local variable referenced before assignment Python
In this post, you will learn how to fix local variable referenced before assignment in the Python programming language.
At the point when you begin defining functions in your code, you will undoubtedly experience an UnboundLocalError sooner or later. This error is raised when you attempt to utilize a variable before it has been assigned in the local context.
In this guide, we talk about what this error means and why it is raised. We stroll through a case of this error in real life to assist you in seeing how you can fix it.
Rules for Python Variable Assignment
Python provides a simple concept to determine the scope of a variable. There are two variable scopes in Python: local and global. A variable is said to be local, if it is assigned in a function. It means that when you define a variable inside a function, you only need to access it inside that function. A variable is declared outside of a function that has global scope. It is accessible throughout the entire program.
Example
x = 1
def myfunc():
if x == 0:
print("Value: Zero")
elif x == 1:
print("Value: One")
elif x >= 2:
print("Value: Greater then 1")
x = 0
myfunc()
Output of the above code-
In the above example, we have defined the variable in two places, at the end of the function and outside of the function. The variable inside the function becomes local to that function. In the above code, we have defined the variable at the bottom of the function, and we are referring to this before the assignment. That's why it returns 'UnboundLocalError'. These are some of the solutions to the issue.
Python local variable referenced before assignment solution 1
This can be solved by changing the scope of the variable that caused the error. If we declare any variable global, then its scope becomes global.
x = 1
def myfunc():
global x
if x == 0:
print("Value: Zero")
elif x == 1:
print("Value: One")
elif x >= 2:
print("Value: Greater then 1")
myfunc()
Output of the above code-
(env) c:\python37\Scripts\projects>test.py
Value: One
Python local variable referenced before assignment solution2
We can also fix this by passing a variable as a parameter to the function, like-
x = 1
def myfunc(x):
if x == 0:
print("Value: Zero")
elif x == 1:
print("Value: One")
elif x >= 2:
print("Value: Greater then 1")
myfunc(x)
Output of the above code-
(env) c:\python37\Scripts\projects>test.py
Value: One
Related Articles
Python Tkinter Combobox Event Binding
Python OpenCV Image Filtering
Python program to print all even numbers between 1 to 100
Remove last element from list Python
Human Body Detection Program In Python OpenCV
Vader Sentiment Analysis Python
isalpha Python
Python YouTube Downloader Script
Python project ideas for beginners
Pandas string to datetime
Fillna Pandas Example
Lemmatization nltk
How to generate QR Code in Python using PyQRCode
OpenCV and OCR Python
PHP code to send SMS to mobile from website
Fibonacci Series Program in Python
Python File Handler - Create, Read, Write, Access, Lock File
Python convert XML to JSON
Python convert xml to dict
Python convert dict to xml