
In this tutorial, we will be discussing why UnboundLocalError: local variable referenced before assignment occurs. When we used functions in our code we come across this type of error as we try to use a local variable referenced before the assignment. Here we will try to understand what this error means, why it is raised and how it can be resolved with the help of examples.
What is UnboundLocalError: local variable referenced before assignment?
In python, the scope of the variable is determined depending on whether the variable is defined inside the function or outside. If the variable is declared inside the function then its scope is within the function itself and we can access it inside the function only.
The error UnboundLocalError: local variable referenced before assignment occurs because we try to assign a value to the variable that does not have a local scope. As the global variable has a global scope the user tries to access to use the variable within the function.
There are two types of variables local variables and global variables.
Difference between local variables and global variables
Local Variables | Global Variables |
local variable can be defined inside the only function. | The global variable can be declared both inside and outside the function |
It can be accessed within the function itself. | It can be accessed from both inside and outside the function or anywhere within the program. |
No need to use keywords to declare it is considered local by default. | The global keyword is used to declare a global variable. |
Example:
The below example code demonstrates the code where the program will end up with the “local variable referenced before assignment” error.
num= 5
def calculate():
num = num + 1
print(num)
calculate()
Explanation:
- We have declared the variable num =5.
- Defined a function calculate() in which we have increased the num variable by 1 and stored it in the variable itself.
- Printed the num value.
- Called the function to calculate.
Error Output:

Explanation:
What went wrong here? We have declared the num variable outside the function while the function cannot access it until you declared it global using the global keyword.
Solution
Solution with the global keyword
num=5
def calculate():
global num
num = num + 1
print(num)
calculate()
Here we initialized the num variable with the global keyword so that it can be accessed inside and outside the function.
Resolved Output:

Solution with passing parameter
Here we can pass the parameter to the function so there is no need of declaring the variable as local or global. So the need for variable declaration is resolved.
def calculate(num):
num = num + 1
print(num)
calculate(5)
Resolved Output:

Summary
Hence, in this article, we came to know the reasons for the error “UnboundLocalError: local variable referenced before assignment”. We have understood how the use of global keywords and passing the parameter solved this type of error.
For more articles on python keep visiting our website copyassignment.com
Thank you for visiting our website.
Also Read:
- What is web development for beginners?
- Microsoft Giving Free Machine Learning Course: Enroll Now
- Accenture Giving Free Developer Certificate in 2023
- Python | Asking the user for input until they give a valid response
- Amazon Summer Internship 2023
- Amazon Giving Free Machine Learning Course with Certificate: Enroll Now
- Google Summer Internship 2023
- 5 Secret ChatGPT skills to make money
- Free Google Certification Courses
- 5 AI tools for coders better than ChatGPT
- New secrets to Earn money with Python in 2023
- How to utilize ChatGPT to improve your coding skills?
- Create your own ChatGPT with Python
- Bakery Management System in Python | Class 12 Project
- SQLite | CRUD Operations in Python
- Event Management System Project in Python
- Ticket Booking and Management in Python
- Radha Krishna using Python Turtle
- Hostel Management System Project in Python
- Sales Management System Project in Python
- Bank Management System Project in C++
- Python Download File from URL | 4 Methods
- Python Programming Examples | Fundamental Programs in Python
- Drawing letter A using Python Turtle
- Spell Checker in Python
- Portfolio Management System in Python
- Stickman Game in Python
- Contact Book project in Python
- Loan Management System Project in Python
- Cab Booking System in Python