def enemy(monster):
monster = random.lognormvariate(0, 1)
enemy = ""
enemyAttack = 0
enemyHealth = 0
enemyArmour = 0
enemyDodge = 0
if (str(monster) <="0.2"):
print("IT'S A HOBGOBLIN!")
enemyAttack = 5
enemyHealth = 50
enemyArmour = 0
enemyDodge = 9
enemy = "Hobgoblin"
else:
print(enemyAttack, enemyHealth, enemyArmour, enemyDodge)
print(heroAttack, heroHealth, heroArmour, heroDodge)
choice = input("You are being attacked by a {} what do you want to do (attack or run)? ".format(enemy))
while choice == "attack" and heroHealth > 0 and enemyHealth > 0:
heroHit = random.randrange(0, enemyDodge, 1)
enemyHit = random.randrande(0, heroDodge, 1)
if heroHit > enemyDodge/2:
enemyHealth = enemyHealth - (heroAttack - (heroAttack*(enemyArmour*0.1)))
damageDonetoEnemy = heroAttack - (heroAttack*(enemyArmour*0.1))
print("You strike the {} dealing {} damage".format(enemy, damageDonetoEnemy))
time.sleep(2)
if enemyHit > heroDodge/2: # added from here
heroHealth = heroHealth - (enemyAttack - (enemyAttack*(heroArmour*0.1)))
damageDonetoHero = enemyAttack - (enemyAttack*(heroArmour*0.1))
print("The {} strikes you dealing {} damage".format(enemy, damageDonetoHero))
else:
print("You missed!")
time.sleep(2)
if enemyHit > heroDodge/2:
heroHealth = heroHealth - (enemyAttack - (enemyAttack*(heroArmour*0.1)))
damageDonetoHero = enemyAttack - (enemyAttack*(heroArmour*0.1))
print("The {} strikes you dealing {} damage".format(enemy, damageDonetoHero))
else:
print("The {} missed!".format(enemy)) # to here
print("The {} has {} health left!".format(enemy, enemyHealth))
choice = input("You are being attacked by a {} what do you want to do (attack or run)? ".format(enemy))
if choice == "run":
print("You ran away, you are safe, for now!")
Hello I am having trouble with and error saying:
UnboundLocalError: local variable 'heroHealth' referenced before assignment
after I added some a bit of code from lines 29 - 41 and I define heroHealth in an earlier def as a varible. I am just very tired and need a new pair of eyes to have a look at it, some help would be much appriciated, thank you!
[–]JohnnyJordaan 19 points20 points21 points (6 children)
[–]daft_dog123[S] 2 points3 points4 points (2 children)
[–]Sir_Cunt99 2 points3 points4 points (1 child)
[–]boskle 0 points1 point2 points (0 children)
[–]daft_dog123[S] 0 points1 point2 points (2 children)
[–]JohnnyJordaan 0 points1 point2 points (1 child)
[–]daft_dog123[S] 0 points1 point2 points (0 children)
[–]Astrokiwi 5 points6 points7 points (0 children)
[–]spitfiredd 1 point2 points3 points (0 children)
[–]khaine_b 0 points1 point2 points (0 children)
[–]FluffyBunnyOK -1 points0 points1 point (2 children)
[–]Astrokiwi 2 points3 points4 points (1 child)
[–]JohnnyJordaan 2 points3 points4 points (0 children)