UnboundLocalError: local variable "user_health" referenced before assignment

Question:

As you may have realized by the obvious signs below, I’m attempting to create a game, a fighting simulation kind of. A very basic one for a class assignment where we have to make a simple game (and I’m probably making it more complicated than it has to be but I wanted to have fun. At the moment, we have the main loop where if the user’s health is greater than zero, which it is at the start (100) he goes on to the first fight and if he gets through all the fights with it still being greater than 100, he wins. If it goes below 100, he loses. My issue is that when testing if health would indeed go low, the user’s health did not because of the following error. I am on python 2.7 if that’s a need to know information.

Traceback (most recent call last):
File "a3.py", line 109, in <module>
fighting_arena()
File "a3.py", line 63, in fighting_arena
menu_loop()
File "a3.py", line 37, in menu_loop
main_loop()
File "a3.py", line 69, in main_loop
easy_fight()
File "a3.py", line 96, in easy_fight
print "You have %s health and Hagen has %s health left." % (user_health, easy_opponent_health)
UnboundLocalError: local variable 'user_health' referenced before assignment

def main_loop():
 global user_health
 user_health = 100
 while user_health > 0:
    easy_fight()
    end_game_victory()
 else:
    end_game_defeat()

def easy_fight():
 easy_opponent_health = 50
 easy_opponent_skills = ['1', '2', '3', '4']
 print '"Your first opponent is Hagen, a germanic gladiator. He bares no armor, but he has a shield and uses a long sword. Beware of his vicious strength!"'
 time.sleep(2)
 print 'You enter the arena surrounded by thousands of Romans demanding blood with Hagen across the field. The fight begins!'
 while easy_opponent_health > 0:
    a = raw_input()
    b = random.choice(easy_opponent_skills)
    if a == "1" and b == "1":
        print "You both slashed each other!"
        user_health -= 5
        easy_opponent_health -= 5
        print "You have %s health and Hagen has %s health left." % (user_health, easy_opponent_health)
    elif a == "1" and b == "2":
        print "You slashed Hagen while he managed to get a non-lethal stab!"
        user_health -= 2.5
        easy_opponent_health -= 5
        print "You have %s health and Hagen has %s health left." % (user_health, easy_opponent_health)
    elif a == "1" and b == "3":
        print "Your slash only scratched Hagen as he dodged it!"
        easy_opponent_health -= 2.5
        print "You have %s health and Hagen has %s health left." % (user_health, easy_opponent_health)
    elif a == "1" and b == "4":
        print "Your slash was blocked by Hagen!"
Asked By: GOAT

||

Answers:

user_health is defined in the main_loop function, and so it can only be accessed from that function unless you globalise it.

Put global user_health before defining user_health = 100:

def main_loop():
    global user_health
    user_heal = 100
    ...
Answered By: TerryA

you assigned user_health = 100 in the scope of main_loop()

but then you used it in easy_fight() so thats why you get the error because its only a variable in main_loop()

a way around this is to globalize the variable by using the global keyword, or create a class and make them class variables

More on variable scope

Answered By: Serial
Categories: questions Tags:
Answers are sorted by their score. The answer accepted by the question owner as the best is marked with
at the top-right corner.