I've been getting an error with a function that is supposed to change a global variable to keep a counter. It's for a game, in this case it would be 'playerHealth' and 'strength'. How can I fix this error?
strength = 1
playerHealth = 100
def orcCombat(playerHealth, playerDamage, strength):
orcHealth = 60
while orcHealth > 0:
if playerHealth > 10:
print "You swing your sword at the orc!, He loses", playerDamage, "health!"
playerHealth = playerHealth - 10
orcHealth = orcHealth - playerDamage
elif playerHealth == 10:
print "The Orc swings a deadly fist and kills you!"
print "Game Over"
else:
print "The Orc has killed you"
print "Game Over"
sys.exit()
if orcHealth <= 0:
print "You killed the Orc!"
print "+1 Strength"
global strength
strength = strength + 1
return "press enter to continue"
error: **name 'strength' is global and local.
Here's the new code. The strength error is fixed, but the global variable playerHealth is not changed, and if I declare playerHealth as a global variable it gives me the error again.
import sys
charisma = 1
strength = 1
intelligence = 1
agility = 1
courage = 1
playerDamage = 20
magic = 0
playerHealth = 100
def orcCombat(playerHealth, playerDamage):
orcHealth = 60
while orcHealth > 0:
if playerHealth > 10:
print "You swing your sword at the orc!, He loses", playerDamage, "health!"
playerHealth = playerHealth - 10
orcHealth = orcHealth - playerDamage
elif playerHealth == 10:
print "The Orc swings a deadly fist and kills you!"
print "Game Over"
else:
print "The Orc has killed you"
print "Game Over"
sys.exit()
if orcHealth <= 0:
print "You killed the Orc!"
print "+1 Strength"
print "HP = ", playerHealth
global strength
strength = strength + 1
return "press enter to continue"
orcCombat(playerHealth, playerDamage)
print strength
print playerHealth
How do I change the function so it changes the global variable playerHealth? Thanks