python - undefined variable 'player_damage' - Python 文本基础游戏

标签 python python-3.x

这是我在 python 中的第二个迷你项目,因为我仍在学习,所以我正在使用引用代码并提出问题来制作该项目,但我在第 49 行不断收到错误“ undefined variable 'player_damage'”- 52.

Sample_FIGHT = {
player_damage: "You desperately try to stop the %s for %i damage",
enemy_damage: "%s gores you for %i damage",
player_win: "The %s collapses with a thunderous boom",
enemy_win: "You are squished"
}

但我确实在函数 Combat 中定义了变量。

def combat (player, enemy):
    player_damage = random.randint (*WEAPONS[player["weapon"]])
    enemy_damage = random.randint(*enemy["attack"])
    player_win = player_damage > enemy["health"]
    enemy_win= enemy_damage  > player["health"]

    return player_damage, player_win , enemy_damage, enemy_win

这是完整的代码。

import random

#initial question to start
print ("The Adventures Of Magical Nadia")
Answer = input ("Do you wish to embark on this adventures?")

#Question to start game or end game
if Answer == "Yes" or Answer == "yes":
  print ("You have accepted the adventure. God Speed my young rass!")
else:
  print ("You are a coward and shall not in bark on a great adventure!")

#function for questions avoide repeat 
def ask(question):
    answer = input(question + " [y/n]")
    return answer in ["y", "Y", "Yes", "YES", "yes"]


#Dic of all the weapons in the game
WEAPONS = {
  "Spear": (3, 10), None:(1,3), "knife":(4,16), "Gun":(16,25), "Glass Bottle":(4,16)
}

#Tracking weapon and player health
player = {"weapon":None, "health": (100)}

#to give the player weapons code
player["weapon"] = "Spear"


#Enemys type
enemy = {"name":None, "health":None, "attack":None }
Gaint_spider = {"health":(45), "attack":(7, 10) } 
Dogs = {"health": (50), "attack":(4,15)}
Dragon = {"health": (150), "attack":(35,45)}

#Function each fight gives random dmg, have a player and enemy, winner and loser

def combat (player, enemy):
    player_damage = random.randint (*WEAPONS[player["weapon"]])
    enemy_damage = random.randint(*enemy["attack"])
    player_win = player_damage > enemy["health"]
    enemy_win= enemy_damage  > player["health"]

    return player_damage, player_win , enemy_damage, enemy_win

#Structure of a fight 
Sample_FIGHT = {
player_damage: "You desperately try to stop the %s for %i damage",
enemy_damage: "%s gores you for %i damage",
player_win: "The %s collapses with a thunderous boom",
enemy_win: "You are squished"
}

# describe the fight in a function

def describe_combat(player, enemy, fight_description):
   player_damage, player_win , enemy_damage, enemy_win = combat(player, enemy)

   print (fight_description["player_damage"] % (enemy["name"], player_damage))
   print (fight_description["enemy_damage"] % (enemy["name"], enemy_damage))

   if player_win:
      print (fight_description["player_win"] % enemy["name"])
      return True

   if enemy_win:
      print (fight_description["player_win"] % enemy["name"])
      return False

      return None # fight is a draw

fight_result = describe_combat(player, Gaint_spider, Sample_FIGHT)
if fight_result is None:
   print ("This is a draw")
elif fight_result: 
   print ("You have won the fight")
else:
   print ("You lost")

最佳答案

您在这里定义了一个字典,因此您需要传递一个应该是字符串的关键变量,例如:

Sample_FIGHT = {
"player_damage": "You desperately try to stop the %s for %i damage",
"enemy_damage": "%s gores you for %i damage",
"player_win": "The %s collapses with a thunderous boom",
"enemy_win": "You are squished"
}

关于python - undefined variable 'player_damage' - Python 文本基础游戏,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/50450279/

相关文章:

python - 在 Python 中模拟滚动 2 个骰子

用于在网站上查找事件端口的 Python 程序?

python - 我的 python 代码不会将视频帧保存为图像

python - make a list based on conditions in python 创建一个唯一的列表

python3,嵌套列表中使用枚举保持索引的最小值

python - 如何识别某个图像何时消失

python - 有没有办法在 Python 中对字符串中的特定序列进行分组?

python - 如何在 pandas 中重置 cumsum streak

python - 在 Python 中绘制 3D 圆柱面图

Python:将驼峰大小写转换为使用正则表达式分隔的空格并考虑首字母缩略词