Why is there an error for z | Sololearn: Learn to code for FREE!
New course! Every coder should learn Generative AI!
Try a free lesson
18th Jul 2020, 6:05 PM
Oma Falk
Oma Falk - avatar
5 Answers
+ 7
Oma Falk In theory, you would want to say globals = locals, rather than the other way around as you want to change the globals dict. You can use globals().update(locals()) instead though.
18th Jul 2020, 8:09 PM
Russ
Russ - avatar
+ 8
z is created in the function, so it is not accessible in the scope of main program. To avoid this, create z at the place where x and y are created, or use global. x=8 y=10 def myadd(): global z z=44 return x+y print(myadd()) print(z)
18th Jul 2020, 6:17 PM
Lothar
Lothar - avatar
+ 4
Oma Falk Well I've never been called that before! Yeah, the normal way of doing things is to say global z as other have pointed out.
18th Jul 2020, 8:24 PM
Russ
Russ - avatar
+ 2
Russ you are a woman whisperer! That's it. (Although I would not survive the day on which I deliver this kind of code to my chief)
18th Jul 2020, 8:15 PM
Oma Falk
Oma Falk - avatar
0
i hoped, locals=globals will do it.
18th Jul 2020, 7:33 PM
Oma Falk
Oma Falk - avatar