文本类游戏切换房间指令无效,求代码排查与解决方案
问题分析与修复方案
核心问题
你遇到的"invalid commands"提示,主要来自三个关键问题:
- 大小写不匹配:输入被转成小写,但字典里的方向键(如
'go East')是首字母大写,导致输入的'go east'不在directions列表中 - 方向判断逻辑错误:拆分指令后取的是单独方向词(如
'east'),但字典里存储方向的键是完整的'go East',导致无法匹配到目标地点 - 代码缩进混乱:原代码中
while循环、if块的缩进不符合Python语法要求,会直接引发运行报错
修复步骤
- 统一大小写匹配:生成
directions列表时同步生成小写版本,用于和用户输入的小写指令匹配 - 修正方向查找逻辑:通过小写匹配找到对应的原方向键(带正确大小写),直接从当前区域字典中获取目标地点
- 修复代码缩进:用4个空格作为统一缩进单位,确保所有代码块层级正确
完整修复代码
areas_dic = { 'Home': {'name': 'Home', 'go East': 'Plains', 'item': 'none'}, 'Plains': {'name': 'Plains', 'go North': 'Rocky Terrain', 'go East': 'Fields', 'go South': 'Stream', 'item': 'Grains'}, 'Stream': {'name': 'Stream', 'go East': 'Forest', 'item': 'Water'}, 'Forest': {'name': 'Forest', 'go West': 'Stream', 'item': 'Firewood'}, 'Fields': {'name': 'Fields', 'go West': 'Plains', 'go North': 'Farmlands', 'item': 'Vegetables'}, 'Rocky Terrain': {'name': 'Rocky Terrain', 'go South': 'Plains', 'go East': 'up the Mountain', 'item': 'Medicinal Flowers'}, 'up the Mountain': {'name': 'up the Mountain', 'go West': 'Rocky Terrain', 'go East': 'Mountain Top', 'item': 'Warmer Clothes'}, 'Farmlands': {'name': 'Farmlands', 'go South': 'Fields', 'go West': 'DYSENTERY', 'item': 'Jerky'}, 'Mountain Top': {'name': 'Mountain Top', 'go West': 'up the Mountain', 'go South': 'DYSENTERY', 'item': 'Milk'} } print('Welcome to Old Americas') print('Object of the game is to collect all 7 items before falling ill') print('You can type go North, go South, go East, go West to move') current_location = 'Home' inventory = [] required_items = {'Grains', 'Water', 'Firewood', 'Vegetables', 'Medicinal Flowers', 'Warmer Clothes', 'Milk'} while True: print(f"You are in the {areas_dic[current_location]['name']}.") # 生成可用方向列表及小写匹配版本 directions = [direction for direction in areas_dic[current_location] if direction.startswith('go ')] directions_lower = [d.lower() for d in directions] print("Available directions:", ", ".join(directions)) # 获取玩家输入 command = input("Enter your command: ").strip().lower() # 处理指令 if command in directions_lower: # 匹配原方向键并切换地点 original_dir = directions[directions_lower.index(command)] current_location = areas_dic[current_location][original_dir] # 处理物品收集 item = areas_dic[current_location].get('item', None) if item and item != 'none': print(f"You found {item}!") inventory.append(item) if item in required_items: required_items.remove(item) if required_items: print(f"Items left to collect: {', '.join(required_items)}") else: print("All items collected!") else: print("Invalid command. Try again.") # 检查是否收集全物品 if not required_items: print("Congratulations! You've collected all items. Now face the villain in DYSENTERY!") break # 检查是否进入最终房间 if current_location == 'DYSENTERY': if not required_items: print("You defeated the illness and survived the journey! Well done!") else: print("You have fallen ill. Game over!") break
额外优化说明
- 增加
item != 'none'判断,避免Home的无效物品被加入背包 - 修正DYSENTERY房间的胜负判断逻辑(原逻辑中背包不会出现'llness',改为检查物品收集进度)
- 统一代码缩进,确保语法合规
内容的提问来源于stack exchange,提问作者PretTyAnd ScaRy
相关产品推荐
相关产品推荐

