0

I'm getting an Error; Variable Referenced Before Assignment for myhp. At the starting of my .py file, I have "myhp = 20" though.

What do I do to get this working?

def fightmode(name, hp, dmg, gold):
    print '\n\n\nYou are in a fight with %s' %name
    print '%s has %sHP' %(name, hp)
    while myhp > 0 and hp > 0:
        print '\n\t1. Attack \n\t2. Guard \n\t3. Run away.'
        opt1= ''
        allowed = ["1", "2", "3"]
        while opt1 not in allowed:
            opt1 = raw_input("\nWhat will you do? ")
            if opt1 == "1":
                hp = hp - mydmg
                print "You have inflicted %d damage on %s. %s's HP is %s" %(mydmg, name, name, hp)
            if opt1 == "2":
                myhp = myhp+5
                print "You are now guarding yourself. Your HP is now %d" %myhp
0

2 Answers 2

2

Insert global myhp at the beginning of the function. If you assign to the variable in the function, Python treats it as local unless you declare it as global.

Sign up to request clarification or add additional context in comments.

1 Comment

I think that did it. But now I'm receiving this error. "can only concatenate list (not int) to list" for my line myhp = myhp + 5
0

I don't see where you have myhp = 20. myhp is a local here, so it has not been assigned. If you want to use the global, put global myhp at the beginning of your function.

1 Comment

this is just a function, I have declared myhp outside of this function. and now I'd like to use that as a local im guessing

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.