a = 17
def test():
print(a)
a = 13
print(a)
test()
This is an error message.
Traceback (most recent call last):
File "sandbox.py", line 6, in <module>
test()
File "sandbox.py", line 3, in test
print(a)
UnboundLocalError: local variable 'a' referenced before assignment
I expected this code will print out 17 13 but an error came out. First print(a) would print 17 because a=13 is not yet executed, and second print(a) would print local a which is 13 because variables are accessed from local to global.
what is wrong in my explanation? It seems I have some misunderstanding.. thanks