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

5
  • 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. Commented Dec 1, 2015 at 14:05
  • Oops, gotta fix that. Thanks for noticing Commented Dec 1, 2015 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. Commented Dec 1, 2015 at 14:15
  • Thats an error when I pasted the code into the question, the actual code is indented properly. Commented Dec 1, 2015 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. Commented Dec 1, 2015 at 14:45

3 Answers 3

2
def orcCombat(playerHealth, playerDamage, strength):

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

def orcCombat(playerHealth, playerDamage):
Sign up to request clarification or add additional context in comments.

3 Comments

It works when I isolate the function. Thanks for the advice, let me see if it works together as a whole.
This worked well to fix one of my problems. I edited the original question to the new problem.
If you want playerHealth to be global too, then delete that from the def line too.
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.

3 Comments

The problem is that when I pass playerHealth or strength as a global variable, it gives me the error 'name is local and global'
Are you declaring it global inside the function?
Just use global once at the beginning of the function.
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.

1 Comment

You have helped me a lot. I have learned a lot from this. I am thankful

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.