Python文本游戏报错:Undefined variable 'player_damage' 求助
问题排查与修复方案
嘿,我看到你遇到的变量未定义错误了,咱们一步步来解决:
核心错误原因:字典键误用变量而非字符串
你代码里的Sample_FIGHT字典犯了一个典型的新手错误——你用了player_damage、enemy_damage这些变量作为字典的键,但这些变量只在combat函数内部定义,在全局作用域里根本不存在,所以Python会报错"Undefined variable"。
字典的键应该是固定的标识符(比如字符串),而不是引用还没定义的变量。你需要把这些键改成字符串形式:
# 修正后的Sample_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" }
额外的几个小问题(避免后续报错)
除了这个核心错误,我还发现几个小问题,提前帮你修复:
Player的health类型错误
你把player的health定义成了元组(100),但实际应该是整数100,不然后续比较enemy_damage > player["health"]会出现类型错误:player = {"weapon":None, "health": 100} # 去掉括号Enemy字典缺少name属性
你的Gaint_spider、Dogs、Dragon字典都没有name键,后续打印描述的时候会显示None,需要补充:Gaint_spider = {"name": "Giant Spider", "health": 45, "attack": (7, 10) } Dogs = {"name": "Wild Dogs", "health": 50, "attack": (4,15)} Dragon = {"name": "Fire Dragon", "health": 150, "attack": (35,45)}describe_combat函数里的逻辑错误
当敌人获胜时,你错误地打印了player_win的描述,应该换成enemy_win:if enemy_win: print(fight_description["enemy_win"]) # 这里修正为enemy_win的描述 return False
修复后的完整代码
把这些修改整合后,你的代码就能正常运行了:
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 embark on a great adventure!") #function for questions avoid 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 = {"name": "Giant Spider", "health": 45, "attack": (7, 10) } Dogs = {"name": "Wild Dogs", "health": 50, "attack": (4,15)} Dragon = {"name": "Fire 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["enemy_win"]) 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")
补充说明
下次遇到变量未定义的错误,先检查这个变量的作用域:如果变量是在函数内部定义的,外部是访问不到的。像你这里的情况,完全不需要用变量当字典键,用字符串作为固定标识才是正确的做法。
内容的提问来源于stack exchange,提问作者BlackHazrd
相关产品推荐
相关产品推荐

