[Tutor] UnboundLocalError and Break

Corey Richardson kb1pkl at aim.com
Thu Oct 1 03:53:52 CEST 2009

Here is my code that is being used/affected by the block that is 
erroring. The rest is superfluous, and does not affect it:

    intel = 10  #Define the abilities.
    strn = 10
    con = 10
    dex = 10
    wis = 10
    exp = 0
    cha = 10
    playHp = 20
    melWep = 1 # A sword
    rngWep = 1 #A bow
    attack = dex+strn/2 #If they hit!
    melDmg = strn+1+melWep/2 #Melee damage
    deff = (con/3)+(dex/2)+6 #Defense
    rngDmg = dex+1+rngWep/2 #Ranged damage
    print "You are hiking through the woods one day, and you are jumped
    by a goblin!" #The beginning of the adventure
    print "Do you want to 1. Attack w/sword, 2. Attack w/bow, 3. Flee,
    or 4. Nothing"
    gob1Hp = 10 #First monsters hitpoints
    def monsAttk (damage, attack,playHp): #A monsters attack  
        if attack > deff:  #Monster hit  
            playHp -= damage #Player takes damage (I think...I will have
    to work that out)
            print "You have sustained", damage, "damage, with",
    damage-playHp, "remaining" #Inform player of their status
    def gmAttack(monHp, monDef, monAgil) :  #A player attack
        op1 = raw_input("Your choice?")
        while op1 != "5":   #Just a loop that you can break out of
    (possibly...working that out)  
            if op1 == '1': #Option 1
                if attack + 10  > monDef : #If the player hits
                    monHp-= melDmg  #Monster takes damage
                    print "you did", melDmg, "damage! It now has",
    monHp, "health left!" #Inform player how much health it has.
                    monsAttk(4, 15) #Monster attacks  
            elif op1 == '2': #Option 2  
                if attack + 10 >monDef: #If you hit the monster
                    monHp -= rngDmg  #Monster takes damage
                    print "you did", rngDmg, "damage! It now has",
    monHp, "health left!"  #Inform player how much health it has  
                    monsAttk(4, 15) #Monster attacks
            elif op1 == '3' : #Option 3
                if attack + dex > monAgil : #If they can escape  
                    print "Thou hast fled!" #They have fled!
                break #Stop the loop (I think. Can someone please inform
    me as of how this works? I have looked online...it makes no sense to me)
            elif op1 == '4' : #Option 4...the dumb one
                monsAttk(4, 15)  #Monster attacks
                monsAttk(4,15) #Monster attacks again, cause they are dumb
                print "You should attack..." #Inform them of their stupidity
            else : #Well, what the hell did they pick?
                print "Unknown option, try again!" #Inform them they
    didn't pick a valid option
    while gob1Hp >= 0: #Well it is still alive
        gmAttack(gob1Hp,13,15) #player attacks
    if gob1Hp <= 0.5 : #if it dies
        print "Gratz! You have gotten 5 exp! You only have", 100-exp,
    "exp left till lv 1!" #print their experience
        exp += 5 #Give them exp

If you catch an error besides what is wrong, please don't point it out, 
as I would like to work it out myself, unless marked in the comments. 
But this one is evading me...I have playHp defined at the beginning, and 
I am getting this error :

    Traceback (most recent call last):
      File "C:\Users\Quick-Start\Documents\Python Doc's\Game_File.py",
    line 183, in <module>
      File "C:\Users\Quick-Start\Documents\Python Doc's\Game_File.py",
    line 167, in gmAttack
        monsAttk(4, 15)
      File "C:\Users\Quick-Start\Documents\Python Doc's\Game_File.py",
    line 157, in monsAttk
        playHp -= damage
    UnboundLocalError: local variable 'playHp' referenced before assignment

If you could please help me, that would be great. I'm just trying to get 
the game engine working, as of now, it is 30% done, with Inventory, 
Equipment, and a more sophisticated and simpler code to come. It's a bit 
of a mess now...

-------------- next part --------------
A non-text attachment was scrubbed...
Name: kb1pkl.vcf
Type: text/x-vcard
Size: 55 bytes
Desc: not available
URL: <http://mail.python.org/pipermail/tutor/attachments/20090930/1de2d75c/attachment.vcf>

More information about the Tutor mailing list