使用全局,但仍未在Python函数中识别

时间:2016-07-15 14:58:59

标签: python

我正在制作一个游戏,你需要对抗怪物,只是为了练习我的编码,当我收到一个我无法理解的错误时。这是代码和我收到的错误:

注意:我将其更改为完整代码,以便您可以看到所有内容。对不起它有多长时间。

import random
import time

player_level = 1
player_HP = 4
player_mana = 2
player_action = "Placeholder"

entity_list = ["Nymph", "Komodo", "Free Soul"]
current_monster_level = player_level
current_monster_HP = random.randrange(current_monster_level, (current_monster_level * 3) + 1)
monster_name = entity_list[random.randrange(0, 3)]

def spawn_monster(level):
    import random
    global current_monster_HP
    global monster_name
    current_monster_HP = random.randrange(level, level * 3)
    monster_name = entity_list[random.randrange(0, 3)]
    print("A ", monster_name, "attacks you!")
    print("The ", monster_name, " has ", current_monster_HP, " HP!")

def attack(level):
    import random
    global monster_name
    global current_monster_HP
    damage = random.randrange(level, level + 4)
    current_monster_HP = current_monster_HP - damage
    if damage == 0:
        print("You missed! No damage done! ", monster_name, "'s HP is ", current_monster_HP, " !")
    elif damage > current_monster_HP:
        damage = current_monster_HP
        print("You did ", damage, " damage! The ", monster_name, " was defeated!")

    else:
        print("You did ", damage, " damage.  The enemy's HP is now ", current_monster_HP, "!")

def spell(level):
    global player_mana
    global player_HP
    player_mana = player_mana - 2
    player_HP = player_HP + level
    print("A spell is cast, and you gain ", level, "HP!")

def monster_retaliate(level):
    import random
    global player_HP
    global monster_name
    global current_monster_HP
    damage = random.randrange(level, level + 2)
    player_HP = player_HP - damage 
    print("The ", monster_name, " attacked you, dealing ", damage, " damage!")
    if damage == 0:
        print("The enemy missed! No damage done! Your HP is ", player_HP, "!")
    elif damage > current_monster_HP:
        damage = current_monster_HP
    else:
        print("the ", monster_name, " did ", damage, " damage. Your HP is now ", player_HP, "!")


print("xxxxxxxxxxxxx")
print("xxxxxxxxxxxxx")
print("x Blind RPG x")
print("xxxxxxxxxxxxx")
print("xxxxxxxxxxxxx")
print("\n")

begin = input("Start a new game?").lower()

if begin == "yes":
    print("Your tale starts when your memory does.  You can't remember anything!")
    time.sleep(1)
    print("You wake up in an empty field, and you think you see a shadowy figure.")
    time.sleep(1)
    print("It slips away into the faraway trees lining the grassy field, or does it?")
    time.sleep(1)
    print("You stand up.")
    time.sleep(1)
    print("You decide to go over to where the shadow left the field.")
    time.sleep(1)
    print("On the ground, you see a small sword.  You pick it up.")
    time.sleep(1)
    print("Suddenly, something jumps out at you from behind a tree.")
    time.sleep(1)

    spawn_monster(1)
    while current_monster_HP > 0:
        player_action = input("What do you do?").lower()
        if player_action == "attack":
            attack(player_level)
            time.sleep(2)
            if current_monster_HP > 0:
                monster_retaliate(1)
            else:
                break
        elif player_action == "cast spell":
            spell(player_level)
            time.sleep(2)
            monster_retaliate(1)
        else:
            print("The enemy attacked you as you stood unmoving!")
            monster_retaliate(1)

        if current_monster_HP <= 0:
            print("You win! Apparently you remember how to fight!")
            break

        if player_HP <= 0:
            print("You died!  If only you had remembered how to fight...")
            break
else:
    print("Then why did you open the game?")

attack()函数有效,spell()函数都有效,只有monster_retaliate()函数导致问题。错误代码是:

  

文件&#34; / Users /(取代我的隐私名称)/ Documents / Blind RPG.py&#34;,第57行,在monster_retaliate

     

player_HP = player_HP - 伤害   NameError:name&#39; player_HP&#39;未定义

我认为这很有趣,因为我使用了global关键字

1 个答案:

答案 0 :(得分:5)

global实际上并不创建变量;它只是表明名称​​应该在全局范围内定义,并在分配时修改它(而不是局部变量)。在尝试访问它之前,您仍需要给它一个值。