The following code gives the error UnboundLocalError: local variable 'Var1' referenced before assignment
:
Var1 = 1
Var2 = 0
def function():
if Var2 == 0 and Var1 > 0:
print("Result 1")
elif Var2 == 1 and Var1 > 0:
print("Result 2")
elif Var1 < 1:
print("Result 3")
Var1 -= 1
function()
How can I fix this?
You can fix this by passing parameters rather than relying on Globals
def function(Var1, Var2):
if Var2 == 0 and Var1 > 0:
print("Result One")
elif Var2 == 1 and Var1 > 0:
print("Result Two")
elif Var1 < 1:
print("Result Three")
return Var1 -= 1
function(1, 1)