Unbound local error with global variable

I am trying to understand why I get UnboundLocalError in my pygame application, Table Wars. Here is a brief description of what is happening:

The variables REDGOLD , REDCOMMAND , BLUEGOLD and BLUECOMMAND initialized as global variables:

 #Red Stat Section REDGOLD = 50 REDCOMMAND = 100 #Blue Stat Section BLUEGOLD = 50 BLUECOMMAND = 100 def main(): [...] global REDGOLD global REDCOMMAND global BLUEGOLD global BLUECOMMAND 

This works when units are unrestored in the main cycle, subtracting the means for the appearance of units.

I'm currently trying to set up the system so that when the unit dies, the killer returns the COMMAND victim and earns GOLD based on the fact that he killed:

 class Red_Infantry(pygame.sprite.Sprite): def __init__(self, screen): [...] self.reward = 15 self.cmdback = 5 [...] def attack(self): if self.target is None: return if self.target.health <= 0: REDGOLD += self.target.reward #These are the problem lines BLUECOMMAND += self.target.cmdback #They will cause the UnboundLocalError #when performed self.target = None if not self.cooldown_ready(): return self.target.health -= self.attack_damage print "Target health: %d" % self.target.health 

This works until the device dies. Then this happens:

 Traceback (most recent call last): File "C:\Users\Oventoaster\Desktop\Games\Table Wars\Table Wars.py", line 606, in <module> main() File "C:\Users\Oventoaster\Desktop\Games\Table Wars\Table Wars.py", line 123, in main RedTeam.update() File "C:\Python27\lib\site-packages\pygame\sprite.py", line 399, in update for s in self.sprites(): s.update(*args) File "C:\Users\Oventoaster\Desktop\Games\Table Wars\Table Wars.py", line 304, in update self.attack() File "C:\Users\Oventoaster\Desktop\Games\Table Wars\Table Wars.py", line 320, in attack REDGOLD += self.target.reward UnboundLocalError: local variable 'REDGOLD' referenced before assignment 

How to get global variables mentioned above using attack block? If this helps, I am using Pygame 2.7.x, so nonlocal will not work: /

+6
source share
3 answers

global make the global variable visible in the current code block. You put the global operator in main , not in attack .

ADDITION

Here is an illustration of the need to use global more than once. Try the following:

 RED=1 def main(): global RED RED += 1 print RED f() def f(): #global RED RED += 1 print RED main() 

You will receive an UnboundLocalError: local variable 'RED' referenced before assignment error message.

Now uncomment the global operator in f and it will work.

The global ad is active in LEXICAL, not in the DYNAMIC area.

+9
source

You need to declare the variable as global in each area where they change

Better yet, find a way to not use global variables. Does it make sense for these attributes to be class attributes?

+3
source

It was found that the variables in main act as read-only global variables in the function. If we try to reassign the value, this will result in an error.

Try:

 #!/usr/bin/env python RED=1 A=[1,2,3,4,5,6] def f(): print A[RED] f() 

This is normal.

But:

 #!/usr/bin/env python RED=1 A=[1,2,3,4,5,6] def f(): print A[RED] A = [1,1,1,1,1] f() 

Create

  File "./test.py", line 6, in f print A[RED] UnboundLocalError: local variable **'A'** referenced before assignment 

and

 #!/usr/bin/env python RED=1 A=[1,2,3,4,5,6] def f(): print A[RED] RED = 2 f() 

Create

  File "./test.py", line 6, in f print A[RED] UnboundLocalError: local variable **'RED'** referenced before assignment 
+1
source

Source: https://habr.com/ru/post/916503/


All Articles