+ 2
global variables
can anyone help me: when we declare a variable global? we do it outside functions or we do it inside, with a declared variable? if i would like to use in game the variable remained_live, and i want, that functions change it during the game, when, and where in the code i should say, it is global?
4 Antworten
+ 1
so, you say, that i have to assign first, then in the body of the function i have to declare it global. that is it?
+ 1
My understanding is that a global variable is defined at the module level. If you write a function within the module and you want that function to have access to a global variable, then you have to call it from within the function using the expression "global". Otherwise, Python treats the variable as local to the function.
Example:
remained_alive = 1
def_function1():
global remained_alive
...
0
x = 10
def my_func ():
global x or whatever #sorry i dont know exactly
if x += 3:
print 'added 3 in 10'
else:
print 'variable not defined'
personally, no idea ..... someone might describe it
0
@michael perez thank you, it helped me.