Polaroid_Mojang
Polaroid_Mojang

Reputation: 3

'variable' is local and global Python

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

Upvotes: 0

Views: 1795

Answers (3)

beoliver
beoliver

Reputation: 5759

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.

Upvotes: 2

m_callens
m_callens

Reputation: 6360

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.

Upvotes: 1

Kevin
Kevin

Reputation: 76184

def orcCombat(playerHealth, playerDamage, strength):

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

def orcCombat(playerHealth, playerDamage):

Upvotes: 2

Related Questions