[Solved] Python Error: local variable ‘var‘ referenced before assignment

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: