UnboundLocalError:赋值前引用的局部变量“ehp”

2024-10-02 00:40:29 发布

您现在位置:Python中文网/ 问答频道 /正文

我第一次使用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

Tags: thetoyouifhaveinthpprint
1条回答
网友
1楼 · 发布于 2024-10-02 00:40:29

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

更好的修复可能涉及根本不使用globals:-)


您可能还应该回顾您的生命点处理,它包含以下内容:

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

告诉玩家他们造成了一定程度的伤害,然后减去一个完全不同的生命值,很可能会让玩家挠头想弄清楚事情是如何运作的:-)

相关问题 更多 >

    热门问题