I'm creating a Galaga clone with pygame and I am having issues with using a value in one function that was modified in other function. I created a very simple version of the code to try to debug it but I have not had any luck.
clock = pygame.time.Clock()
var1 = 10
var1 = var1 + 2
gameExit = False
while not gameExit:
Here is "correct" way of doing what you want:
var1 = 10 def function3(): global var1 var1 = var1 + 2 print var1 function3() print var1
Your code is modifying local variable var1 which is only defined within your function3. I put word correct in quotes because global variable is (almost) never a good idea. If you need state (variable) shared between multiple functions, you could create a class. Or you could be modern functional and immutable and have the function take a value as argument, modify and return it.