+ 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?

2nd Oct 2016, 7:34 AM
Benedek Máté Tóth
Benedek Máté Tóth - avatar
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?
2nd Oct 2016, 10:05 AM
Benedek Máté Tóth
Benedek Máté Tóth - avatar
+ 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 ...
3rd Oct 2016, 10:43 PM
Michael Perez
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
2nd Oct 2016, 8:01 AM
Eric john
Eric john - avatar
0
@michael perez thank you, it helped me.
4th Oct 2016, 4:01 PM
Benedek Máté Tóth
Benedek Máté Tóth - avatar