未绑定错误,在赋值之前引用的局部变量

时间:2017-04-05 18:49:46

标签: python python-3.x

我正在制作一个有趣的小文字游戏。我想使用一个函数,它位于我所谓的函​​数文件中。

有问题的函数attack()不起作用,程序崩溃并出现错误:

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

functionala文件的新版本和改进版似乎是导致问题的原因:

variablestamina = 20
variablehealth = 40
variablemonsterhealth = 30
variableattacktype1 = ("Lightattack")
variableattacktype2 = ("Mediumattack")
variableattacktype3 = ("Heavyattack")

def attack():
     variableattackquery = input("You can execute three types of attacks. Lightattack does 2 damage and takes no stamina. Mediumattack does 4 damage and takes 2 stamina. Heavyattack does 7 damage and takes 5 stamina. You can only do one 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 you for "+str(monsterattack))
     print ("You have "+str(variablehealth)+" health left")
     print()

1 个答案:

答案 0 :(得分:0)

这似乎是一种更简洁的方式,所有这些都在一个文件中。你可能想看看使用类。

在控制台中,拨打game()以启动游戏,即可。当怪物或你有健康&lt; = 0时游戏结束。

代码:

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:
        a = input('you can execute 3 types of attacks, light, medium or heavy... pick one.')
        a = a.lower().strip()
        if a 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 you for {}'.format(monster,ma))
    print('you have {} health left'.format(health))
    return stamina, health, monsterhealth

注意:即使在单个文件中执行此操作,您也需要将变量范围限定为&#34; main&#34;过程(game),并将它们传递给attack函数。否则,引用这些名称将引发相同的错误,您可以像这样重现:

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

然而,这可能令人困惑,以下会引发错误:

m = 1
def foo():
   print m

这也不会:

m = 1
def foo():
   a = m
   print a

但是这两者看起来都很糟糕,通常最好将值从主过程传递给被调用的函数/方法/等,并将适当的值返回给调用者。