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

问题描述

老实说,我真的不确定问题是什么。我想做的是从enemyhp中减去attackdmg。据我所知,一切都是正确的。我不断收到以下回溯的错误:

Traceback (most recent call last):
  File "/tmp/sessions/95bd63b8a3cf1ead/main.py", line 71, in <module>
    attack()
  File "/tmp/sessions/95bd63b8a3cf1ead/main.py", line 46, in attack
    enemyhp = enemyhp - attackdmg
UnboundLocalError: local variable 'enemyhp' referenced before assignment

我对python很陌生所以是的对不起

import sys,time,random
global enemyhp
global playerhp


print("""
░░ ▄▄ ▒█▀▀█ ▒█▀▀█ ▒█▀▀█   ▀▀█▀▀ ▒█░▒█ ▀█▀ ▒█▄░▒█ ▒█▀▀█ ▒█▀▀▀█ ▄▄ ░░ 
▀▀ ▄▄ ▒█▄▄▀ ▒█▄▄█ ▒█░▄▄   ░▒█░░ ▒█▀▀█ ▒█░ ▒█▒█▒█ ▒█░▄▄ ▒█░░▒█ ▄▄ ▀▀ 
░░ ░░ ▒█░▒█ ▒█░░░ ▒█▄▄█   ░▒█░░ ▒█░▒█ ▄█▄ ▒█░░▀█ ▒█▄▄█ ▒█▄▄▄█ ░░ ░░ 

                RPG BATTLE THINGY-WINGO

""")

monstertypes = ["BATTY McBATFACE", "SKELETON", "BOSS BAT", "SUPER SKELETON"]
playerhp = 100

#set ID of the attacker
monstername = random.choice(monstertypes)
if monstername == "BATTY McBATFACE":
    enemyhp = 10
elif monstername == "SKELETON":
    enemyhp = 25
elif monstername == "BOSS BAT":
    enemyhp = 50
elif monstername == "SUPER SKELETON":
    enemyhp = 80


typing_speed = 50 #wpm
def slow_type(t, d):
    for l in t:
        sys.stdout.write(l)
        sys.stdout.flush()
        if d == "":
          d = 1
        time.sleep(d / 10)
    print("")


def attack():
    print("You attack the", monstername + "!")
    attackdmg = random.randint(5, 20)
    time.sleep(1)
    if not random.randint(1, 4) == 4:
        enemyhp = enemyhp - attackdmg
        if attackdmg >= 15:
            slow_type("THWACK!", 0.5)
            print(attackdmg, "HP of critical damage to the", monstername + "!")
        elif attackdmg >= enemyhp:
            time.sleep(0.5)
            slow_type("KAPOW!", 0.5)
            print(attackdmg, "HP of mortal damage to the", monstername + "!")
        else:
            time.sleep(0.5)
            print(attackdmg, "HP of damage to the", monstername + "!")

    else:
        print("The attack missed!")


approachmsg = ["drew near!", "is in the way!" "attacks!", "approaches...", "wants to fight!", "bumped into you!"]
print(monstername, random.choice(approachmsg))
fighting = True

while fighting:
    print("Do you Fight or Run?")
    battlecommand = input()

    if battlecommand == "Fight" or battlecommand == "fight":
        attack()
    elif battlecommand == "Run" or battlecommand == "run":
        canrun = random.choice([True, False])
        slow_type(". . .", 5)
        if canrun:

            fighting = False
        else:
            print("couldn't get away...")
    else:
      print("please type a valid command!")
if playerhp < 1:
    print("""
█▀▀▀ █▀▀█ █▀▄▀█ █▀▀   █▀▀█ ▀█░█▀ █▀▀ █▀▀█
█░▀█ █▄▄█ █░▀░█ █▀▀   █░░█ ░█▄█░ █▀▀ █▄▄▀
▀▀▀▀ ▀░░▀ ▀░░░▀ ▀▀▀   ▀▀▀▀ ░░▀░░ ▀▀▀ ▀░▀▀
    """)
else:
  slow_type("you escaped!", 0.2)


它应该只是将attackdmg从敌人身上带走,但我真的不确定为什么会这样出错。

标签: python

解决方案


使用全局变量有一点“不好的风格”......你需要告诉python你的意思是修改全局 enemyhp

der attack():
    global enemyhp  # you want to modify the global one
    # ... rest of code...

如果你把它排除在外,它会enemyhp在本地函数范围内寻找并且找不到它:因此“在声明之前使用”。

阅读全局变量很好,你不能修改它们。


推荐阅读