python - Unbound error, local variable referenced before assignment -


i'm making small text game fun. want use function located in function file made called functionala.

the function in question, attack(), not work , program crashes error:

traceback (most recent call last):   file "c:\users\seanm\desktop\programming\the mists of alandria\mists_of_alandria.py", line 22, in <module>     functionala2.attack()   file "c:\users\seanm\desktop\programming\the mists of alandria\functionala2.py", line 27, in attack     variablestamina += 2 unboundlocalerror: local variable 'variablestamina' referenced before assignment 

the new , improved version of functionala file seems causing problem:

variablestamina = 20 variablehealth = 40 variablemonsterhealth = 30 variableattacktype1 = ("lightattack") variableattacktype2 = ("mediumattack") variableattacktype3 = ("heavyattack")  def attack():      variableattackquery = input("you can execute 3 types of attacks. lightattack 2 damage , takes no stamina. mediumattack 4 damage , takes 2 stamina. heavyattack 7 damage , takes 5 stamina. can 1 per turn: ")      if variableattackquery == variableattacktype1:         variablemonsterhealth -= 2         variablestamina -= 2      if variableattackquery == variableattacktype2:         variablemonsterhealth -= 4         variablestamina -= 4      if variableattackquery == variableattacktype3:         variablemonsterhealth -= 7         variablestamina -= 7       variablestamina += 2      variablestamina = min(20, variablestamina)      print ("the "+monster+" has "+str(variablemonsterhealth)+" health left")      print ("you have "+str(variablestamina)+" stamina left")      monsterattack = random.randrange(4,6)      variablehealth -= monsterattack      print ("the "+monster+" attacks "+str(monsterattack))      print ("you have "+str(variablehealth)+" health left")      print() 

this seems cleaner way of doing it, in single file. may want @ using classes.

from console, call game() start game, that's it. game end when either monster or have health <= 0.

code:

from random import randrange  def game():     stamina = 20     health = 40     monsterhealth = 30     monster = 'orc'     attacks = {'light':(-2,0),'medium':(-4,-2),'heavy':(-7,-4)}     while true:         = input('you can execute 3 types of attacks, light, medium or heavy... pick one.')         = a.lower().strip()         if in attacks:             stamina, health, monsterhealth = attack(stamina, health, monsterhealth, monster, attacks[a])             if stamina <= 0:                 print 'you have died...'                 break             elif monsterhealth <= 0:                 print 'the {} has died...'.format(monster)                 break         else:             break  def attack(stamina, health, monsterhealth, monster, att):     monsterhealth += att[0]     stamina += att[1]     stamina = min(20, stamina)     print('the {} has {} health remaining'.format(monster,monsterhealth))     print('you have {} stamina remaining'.format(stamina))     ma = randrange(4,6)     health -= ma     print('the {} attacks {}'.format(monster,ma))     print('you have {} health left'.format(health))     return stamina, health, monsterhealth 

nb: doing in single file, need scope variables "main" procedure (game), , pass them attack function. otherwise, referring these names raise same error, , can reproduce so:

m = 1 def foo():    m += 1  '## m doesn't exist within scope of foo, raise same error 

however, , may confusing, following not raise error:

m = 1 def foo():    print m 

nor this:

m = 1 def foo():    = m    print 

but both of seem kind of hack-y , it's better pass values main procedure called functions/methods/etc , return appropriate values caller.


Comments