I encounter no problems running this code:
x = 1
def func():
print(x + 1)
func()
2
But when I run this:
x = 1
def func():
try:
x += 1
except:
pass
print(x + 1)
func()
An error pops:
UnboundLocalError: cannot access local variable 'x' where it is not associated with a value
I am not asking if I can modify global variables in functions or not. I used try-except keywords to avoid that error. After python caught the exception, I thought nothing would change and print(x + 1)
would work like the first code. So what changed in the scope during the try-except part that x is no longer accessible?