I have a global variable that I named Y_VAL, which is initialized with a value of 2.
Then I have a function called f () (for short) that uses Y_VAL.
def f():
y = Y_VAL
Y_VAL += 2
However, when I try to run my python code, it gives an error message:
UnboundLocalError: local variable 'Y_VAL' referenced before assignment
If you delete the last line Y_VAL += 2, it works fine.
Why does python think that Y_VAL is a local variable?
source
share