If i want to update the valve of global variable inside the function than how i can do this | Sololearn: Learn to code for FREE!
New course! Every coder should learn Generative AI!
Try a free lesson
+ 5

If i want to update the valve of global variable inside the function than how i can do this

You can take given below example to explain clearly x=10 def glb(num): #here I want to update value of x # if i write x+=20 its shows error why?? l=15 print(num,l,x) glb(10)

13th Jun 2020, 2:32 PM
Runtime Terror
Runtime Terror - avatar
1 Answer
+ 3
Because x is not defined inside the function. You can enter: x = 10 def glb(num): global x x += 20 l = 15 print(num, l, x) Its not usually the best idea, but it'll work. What exactly are you trying to do? the variable num isnt even being used
13th Jun 2020, 2:41 PM
Slick
Slick - avatar