I would like to test the example of the use of the nonlocal statement specified in the answer on this question:
def outer():
x = 1
def inner():
nonlocal x
x = 2
print("inner:", x)
inner()
print("outer:", x)
but when I try to load this code, I always get a syntax error:
Traceback (most recent call last):
File "<stdin>", line 1, in <module>
File "t.py", line 4
nonlocal x
^
SyntaxError: invalid syntax
Does anybody know what I am doing wrong here (I get the syntax error for every example that I use, containing nonlocal
).