Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Unbound Local Error with global variable

I am trying to figure out why I get an UnboundLocalError in my pygame application, Table Wars. Here is a summary of what happens:

The variables, REDGOLD, REDCOMMAND, BLUEGOLD and BLUECOMMAND, are initialised 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 spawning units within the main loop, subtracting funds to spawn units.

Right now, I am trying to set up a system so that when a unit dies, the killer refunds the victim's COMMAND and earns GOLD based on what 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's health: %d" % self.target.health

This works right up until the unit 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 do I get the global variables mentioned above to change with the attack block? If it helps, I am using Pygame 2.7.x, so nonlocal won't work :/

like image 939
Oventoaster Avatar asked May 23 '12 22:05

Oventoaster


People also ask

How do you fix unbound local variables?

The Python "UnboundLocalError: Local variable referenced before assignment" occurs when we reference a local variable before assigning a value to it in a function. To solve the error, mark the variable as global in the function definition, e.g. global my_var .

What does an unbound local error mean?

The UnboundLocalError: local variable referenced before assignment error is raised when you try to assign a value to a local variable before it has been declared. You can solve this error by ensuring that a local variable is declared before you assign it a value.

Do global variables override local?

LOCAL variables are only accessible in the function itself. So, in layman's terms, the GLOBAL variable would supersede the LOCAL variable in your FIRST code above.

What does it mean if a variable is unbound in Python?

A symbol that has not been given a value by assignment or in a function call is said to be “unbound.”


1 Answers

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

ADDENDUM

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

RED=1

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

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

main()

You will get the error UnboundLocalError: local variable 'RED' referenced before assignment.

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

The global declaration is active in a LEXICAL, not a DYNAMIC scope.

like image 106
Ray Toal Avatar answered Sep 30 '22 20:09

Ray Toal