0

I'm half way through a chance fighting program and I wanted to shorten my work by using functions. But I got the error,

UnboundLocalError: local variable 'Ehealth' referenced before assignment 

This is my code so far...

import random
import sys
import time

print ("You encounter a wild boar.")

time.sleep(1)

Weapon = input("Do you use a 'Bow and Arrow' or a 'Sword'.")

Ehealth = (100)
health = (100)
Ealive = (1)
alive = (1)

def attack():
    damage = (random.randrange(5,21))
    time.sleep(3)

    print ("You attack the Boar for " + str(damage) + " attack points.")
    time.sleep(3)

    Ehealth = (Ehealth - damage)

    print ("The Boars health is at " + str(Ehealth) + ".")
    time.sleep(3)

if Weapon == ("Bow and Arrow"):
    Emiss = (20)    #out of 40
    miss = (15)     #out of 40

    Espeed = (random.randrange(1,11))
    speed = (random.randrange(1,11))

    if Espeed > (speed):
        print ("The Boar is faster than you so he attacks first.")
        time.sleep(3)

        print ("Your health is at " + str(health) + " and the Boars health is at " + str(Ehealth) + ".")
        time.sleep(3)

        while (alive == 1):      #1 = alive, 2 = dead
            Emiss = (random.randrange(1,41))
            if Emiss < (20):

                print ("The Boar missed.")

                attack()

                if Ehealth > (0):
                    alive = (1)

                    continue

                else:
                    alive = (2)
                    print ("You Won!")

                    sys.exit()



            Edamage = (random.randrange(5,16))

            print ("The Boar attacks you with " + str(Edamage) + " attack points.")
            time.sleep(4)

            health = (health - Edamage)
            time.sleep(4)

            print ("Your health is at " + str(health) + ".")
            time.sleep(4)

            if alive <= (0):
                print ("You died...")
                sys.exit()

            attack()

            if Ehealth > (0):
                alive = (1)

            else:
                alive = (2)
                print ("You Won!")

                sys.exit()

I got the error at the line on

Ehealth = (Ehealth - damage)

Any help would be appreciated.

Sam
  • 65
  • 4
  • 7

3 Answers3

1

You are trying to use the variable that lies outside of the function. In any case I'd do this:

def attack():
    global Ehealth
    damage = (random.randrange(5,21))
    time.sleep(3)

    print ("You attack the Boar for " + str(damage) + " attack points.")
    time.sleep(3)

    Ehealth = (Ehealth - damage)

    print ("The Boars health is at " + str(Ehealth) + ".")
    time.sleep(3)

Do note the 'global' keyword is needed if you're changing the value of the variable.

Haifeng Zhang
  • 30,077
  • 19
  • 81
  • 125
ZetaRift
  • 332
  • 1
  • 9
0

You are assigning to that variable in the attack() function:

Ehealth = (Ehealth - damage)

Assignment in a function makes a name local; you appear to expect it to be a global instead. Because it is a local, and hasn't been assigned to in the function before that line, you get your error.

Tell Python to treat it as a global instead. Add this line to your function (as the first line is probably a good idea):

global Ehealth

This tells the Python compiler to treat Ehealth as a global within your function, even though you assign to it.

Martijn Pieters
  • 1,048,767
  • 296
  • 4,058
  • 3,343
0

Ehealth is global variable, If you just print it then there wont be any error But when you try to modify it, function consider it as local variable. Solution :-

def attack(Ehealth=Ehealth):
AlokThakur
  • 3,599
  • 1
  • 19
  • 32