0

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

  • Your indentation looks a little funky. Is `orcHealth = 60` supposed to have the same amount of spacing as `def orcCombat`? That means it's outside the function. – Kevin Dec 01 '15 at 14:05
  • Oops, gotta fix that. Thanks for noticing – Polaroid_Mojang Dec 01 '15 at 14:13
  • 1
    I think you still have some indentation issues, the code starting at `orcHealth = 60` should be 4 spaces indented if it is the body of the "orcCombat" function. – toti08 Dec 01 '15 at 14:15
  • Thats an error when I pasted the code into the question, the actual code is indented properly. – Polaroid_Mojang Dec 01 '15 at 14:29
  • `strength` is always intended to be a global, whether or not the code that updates it is reached or not. Move `global strength` to the beginning of the function. – chepner Dec 01 '15 at 14:45

3 Answers3

2
def orcCombat(playerHealth, playerDamage, strength):

You don't need to pass global variables as arguments to your function. Try:

def orcCombat(playerHealth, playerDamage):
Kevin
  • 74,910
  • 12
  • 133
  • 166
2

when you pass the variable 'strength' or in this case foo to a function, the function creates local scope in which foo refers to the variable passed in the context of the function test1.

>>> foo = 1
>>> def test1(foo):
...   foo = foo + 1
...   return
...
>>> test1(foo)
>>> foo
1

As you can see, the 'global' foo has not changed. In this second version we use the global keyword.

>>> def test2():
...   global foo
...   foo = foo + 1
...   return
...
>>> foo
1
>>> test2()
>>> foo
2

have a look at this question

Another option is to pass the the variable foo to the function as you have, when you return from the function you just return the variable foo along with any other variables using a tuple.

Community
  • 1
  • 1
beoliver
  • 5,579
  • 5
  • 36
  • 72
1

The error is occurring because of a naming conflict between the strength parameter of the function and the global variables strength. Either rename the function parameter or remove it entirely.

m_callens
  • 6,100
  • 8
  • 32
  • 54