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.