How to add global variables?

14th Dec 2019, 6:21 PM
Arydev - avatar
2 Answers
Simply when you define a variable inside a function you can't acess to it outside the function and if you define a variable outside the function thats global variable and you access to it inside the function , example: def foo(): x='local' Print(x) This gives you error becuase x is local and tried to access to it outside the function And : x="global" def foo(): print(x) It prints x beacause x is global as you called it
14th Dec 2019, 10:53 PM
Sahand Jalali
Sahand Jalali - avatar
def f(): global x = 123 print(x) # it's print 123
14th Dec 2019, 11:47 PM
Backerbak - avatar