2
我正在製作一個遊戲,您需要與怪物戰鬥,只是爲了練習我的編碼,當我遇到一個我無法理解的錯誤時。以下是代碼和我收到的錯誤:使用全局,但仍然沒有在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()
功能,是造成麻煩。錯誤代碼是:
File "/Users/(Replacing my name for privacy)/Documents/Blind RPG.py", line 57, in monster_retaliate
player_HP = player_HP - damage NameError: name 'player_HP' is not defined
我認爲這是有趣的,看到我用global
關鍵字
如果您想練習您的編碼技能,請在沒有全局變量的情況下嘗試。全局造成問題,使代碼不穩定,難以理解和修復。參數是你的朋友;) –