簡體   English   中英

未綁定錯誤,分配前已引用局部變量

[英]Unbound error, local variable referenced before assignment

我正在做一個小型的文字游戲,很有趣。 我想使用位於我創建的稱為Functionala的功能文件中的功能。

有問題的函數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()

這似乎是一種更干凈的方法,全部都在一個文件中。 您可能想看看使用類。

從控制台中,調用game()即可開始游戲。 當怪物或您的生命值小於等於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

注意:即使在單個文件中執行此操作,也需要將變量的作用域限定在“主要”過程( 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

但是,這兩種方法似乎都是很不禮貌的,通常最好將值從主過程傳遞給被調用的函數/方法/ etc,然后將適當的值返回給調用者。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM