I am learning Python. A book on Python 3 says the following code should work fine:
def funky():
print(myvar)
myvar = 20
print(myvar)
myvar = 10
funky()
But when I run it in Python 3.3, I got the
UnboundLocalError: local variable 'myvar' referenced before assignment
error. My understanding is that the first print(myvar)
in funky
should be 10 since it's a global variable. The second print(myvar)
should be 20 since a local myvar
is defined to be 20. What is going on here? Please help clarify.