首页 > 解决方案 > UnboundLocalError:分配前引用的局部变量“ehp”

问题描述

我第一次使用python编写了一个糟糕的基于文本的游戏,我在制作战斗功能时遇到了问题。每次遇到敌人时都会调用它,并且变量将在每次战斗之前定义。

我遇到的问题是我的变量“ehp”在分配变量之前被引用。(代表敌人生命值)。我的代码在下面列出,我希望得到一些帮助,了解如何更改我的代码以防止我的程序出现错误代码。

import random

hp = int(20)
ehp = int(10)
def fight():
    print("You have encountered",(enemy))
    if speed >= espeed:
        first = "c"
    for x in range(100):
        if ehp <= 0:
            print("You have won!")
            break
        elif hp <= 0:
            print("You have died!")
            break
        else:
            print("1: Light Attack")
            print("2: Heavy Attack")
            print("3: Dodge")
            attack = input("1/2/3: ")
            if attack == "1":
                print("You have used, Light Attack!")
                lightdam = (random.randint(0,damage/2))
                print("You have inflicted,",edam,"to",enemy)
                ehp = ehp - (random.randint(0,damage/2))
                print("Enemy Health:",ehp)
                print(character,"Health:",hp)
                print(enemy,"Has used attack!")
                eattack = (random.randint(0,edam/2))
                print(enemy,"Has inflicted",eattack,"damage!")
                hp = hp - eattack
                print("Enemy Health:",ehp)
                print(character,"Health:",hp)
            elif attack == "2":
                print("You have used, Heavy Attack!")
                heavydam = (random.randint(0,damage))
                print("You have inflicted,",heavydam,"to",enemy)
                ehp = ehp - (random.randint(0,damage))
                print("Enemy Health:",ehp)
                print(character,"Health:",hp)
                print(enemy,"Has used attack!")
                eattack = (random.randint(0,edam))
                print(enemy,"Has inflicted",eattack,"damage!")
                hp = hp - eattack
                print("Enemy Health:",ehp)
                print(character,"Health:",hp)

print("Welcome to the tales of Iryophia, please enter your characters name.")
character = input("Character name: ")
print("Garnier the Honorable:")
print("Welcome to the city of Iryophia, do you remember how you got here?")
y0 = input("Y/N: ")
for x in range(6):
    if y0 == "N":
        print("Garnier the Honorable:")
        print("Well",character,", all I can remember is a certain man entering a neighbouring town, and well, I'm not sure how to put this, but, you were killed.")
        print("I understand how crazy this may sound but you were brought back to life. You would have lost all of your memory, but, you are alive!")
        print("Do you remember the name of the man who killed you?")
        nemesis = input("Nemesis: ")
        print("Garnier the Honorable:")
        print("Ah yes, I remember now,",nemesis,"was his name.")
        break
    if y0 == "Y":
        print("Garnier the Honorable:")
        print("Okay, well the man that attacked you, what was his name?")
        nemesis = input("Nemesis: ")
        print("Garnier the Honorable:")
        print("Ah yes, I remember now,",nemesis,"was his name.")
        break

print("Come back with me to my home.")
print("")
print("Garnier the Honorable:")
print("I have a bow, an axe, or a sword for you. Which one do you pick?")
weapon = input("Bow/Axe/Sword: ")
for x in range(6):
    if weapon == "Bow":
        damage = int(3)
        speed = int(5)
        break
    if weapon == "Axe":
        damage = int(7)
        speed = int(3)
        break
    if weapon == "Sword":
        damage = int(5)
        speed = (4)
        break
print("You have collected:",weapon+"!")
print("Damage:",damage)
print("Speed:",(speed))

print("Garnier the Honorable:")
print("Would you like to have a practice fight?")
fight0 = input("Y/N: ")
for x in range(6):
    if fight0 == "Y":
        ehp = int(10)
        enemy = "Garnier the Honorable"
        espeed = int(3)
        edam = int(4)
        fight()
        break

标签: python

解决方案


检查这两个代码行fight()至少这两个,尽管可能还有其他行):

ehp = ehp - (random.randint(0,damage/2))
hp = hp - eattack

对于未明确标记为全局的变量,Python 做了一些假设:

  • 如果您只使用该变量,它将通过不同的级别跟踪范围,直到找到匹配的名称;和
  • 如果您在函数中的任何位置设置或更改它,则它在函数中的任何位置都被视为局部变量

因此,一个简单的解决方法是在函数中显式地将其标记为全局:

def fight():
    global ehp
    global hp
    print("You have encountered",(enemy))
    :
    and so on

更好的解决方法可能是根本不使用全局变量 :-)


您可能还应该查看您的命中点处理,其中包含以下内容:

heavydam = (random.randint(0,damage))
print("You have inflicted,",heavydam,"to",enemy)
ehp = ehp - (random.randint(0,damage))

告诉玩家他们造成了一定程度的伤害,然后减去完全不同数量的生命值,这可能会让玩家摸不着头脑,试图弄清楚事情是如何运作的 :-)


推荐阅读