Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/python/344.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Python UnboundLocalError:局部变量';ehp&x27;分配前参考_Python - Fatal编程技术网

Python UnboundLocalError:局部变量';ehp&x27;分配前参考

Python UnboundLocalError:局部变量';ehp&x27;分配前参考,python,Python,我第一次使用python编写了一个糟糕的基于文本的游戏,在为一场战斗编写函数时遇到了一个问题。它将在你每次遇到敌人时调用,并且变量将在每次战斗前定义 我遇到的问题是,我的变量“ehp”在赋值变量之前被引用。(代表敌人的生命点)。下面列出了我的代码,我希望得到一些帮助,了解如何更改代码以防止程序中出现错误代码 import random hp = int(20) ehp = int(10) def fight(): print("You have encountered&quo

我第一次使用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
随机导入
hp=int(20)
ehp=int(10)
定义战斗():
打印(“你遇到过”(敌人))
如果速度>=espeed:
first=“c”
对于范围(100)内的x:

如果ehp检查
fight()
中的这两行代码(至少这两行,尽管可能还有其他代码):

对于未显式标记为全局的变量,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))
告诉玩家他们造成了一定程度的伤害,然后减去一个完全不同的生命值,很可能会让玩家挠头想弄清楚事情是怎么回事:-)

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