+ 1

Nonlocal Keyward Error, how to solve?

def myfunc1(): x = "John" def myfunc2(): nonlocal x x = "hello" myfunc2() return x

3rd Feb 2019, 5:27 PM
L. Yeghiazaryan🇦🇲
L.  Yeghiazaryan🇦🇲 - avatar
7 Antworten
+ 5
Levon Yeghiazaryan, which python version do you use?
3rd Feb 2019, 5:41 PM
Anna
Anna - avatar
+ 5
Levon Yeghiazaryan Yes, that explains a lot 😏 Nonlocal will only work with python 3
3rd Feb 2019, 5:50 PM
Anna
Anna - avatar
+ 2
Try running your code in the Code Playground. It works fine for me.
3rd Feb 2019, 5:36 PM
giannismach
giannismach - avatar
+ 2
2.7, I guess the reason of your question.)
3rd Feb 2019, 5:48 PM
L. Yeghiazaryan🇦🇲
L.  Yeghiazaryan🇦🇲 - avatar
+ 1
Thank you, I will do.
3rd Feb 2019, 5:39 PM
L. Yeghiazaryan🇦🇲
L.  Yeghiazaryan🇦🇲 - avatar
0
It works normally for me, it returns 'hello' when called. It doesn't raise any error.
3rd Feb 2019, 5:31 PM
giannismach
giannismach - avatar
0
It raises invalid syntax on nonlocal x
3rd Feb 2019, 5:34 PM
L. Yeghiazaryan🇦🇲
L.  Yeghiazaryan🇦🇲 - avatar