未定义的变量' player_damage' - Python文本基础游戏

时间:2018-05-21 13:43:45

标签: python python-3.x

所以这是我在python中的第二个迷你项目,因为我还在学习我使用参考代码并提出问题来制作项目,但我一直收到错误" Undefined variable& #39; player_damage'"在第49-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中定义了varibale。

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")

2 个答案:

答案 0 :(得分:3)

你在这里定义一个字典,所以你需要传递一个应该是字符串的键变量,如:

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"
}

答案 1 :(得分:0)

你没有任何已定义的关键变量。这是错误的原因。 所以,只需在以下代码之前初始化它们

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"
}