Python文本办公室逃脱游戏异常求助:无法拾取物品+非法移动崩溃
办公室逃脱游戏调试求助与修复方案
当前开发的文本类办公室逃脱游戏存在两个核心问题:
- 无法拾取房间内的物品
- 合法移动时房间名称显示正常,但执行非法移动时程序直接崩溃
已尝试过用try/except替代if/else、重命名更换变量等调试方法,问题仍未解决,相关代码如下:
# Lists the rooms on the map rooms = { 'Cubicle': {'North': 'Copy Room', 'East': 'Coat Room', 'West': 'Break Room'}, 'Break Room': {'North': 'Restroom', 'East': 'Cubicle', 'Item': 'Backpack'}, 'Restroom': {'South': 'Break Room', 'Item': 'Hat'}, 'Copy Room': {'South': 'Cubicle', 'East': 'File Room', 'Item': 'Keys'}, 'File Room': {'West': 'Copy Room', 'South': 'Coat Room', 'East': 'Office', 'Item': 'Phone'}, 'Coat Room': {'West': 'Cubicle', 'North': 'File Room', 'Item': 'Coat'}, 'Office': {'West': 'File Room', 'South': 'Stairwell', 'Item': 'Keycard'}, 'Stairwell': {'North': 'Office', 'Villain': 'Boss'}, } # Keeps track of inventory inventory = [] nearby_item = ' ' # Keeps track of current room current_room = 'Cubicle' while True: print(f'You are in the {current_room}\nInventory : {inventory}\n{"-" * 27}') # Checks for item in the room if 'Item' in rooms[current_room].keys(): nearby_item = rooms[current_room]["Item"] if nearby_item not in inventory: # If item name is plural if nearby_item[-1] == 's': print(f'You see {nearby_item}') # If item name is singular else: print(f'You see a {nearby_item}') # Boss encounter if 'Boss' in rooms[current_room].keys(): # Lose condition if len(inventory) < 6: print(f'You were caught by the Boss and now you will have to work all weekend') break # Win condition else: print(f'You were able to sneak by the boss with your hat pulled down low') print(f'and with the phone to your ear you looked too busy to interrupt') print(f'Congratulations you have escaped the office!') break # User Movement user_input = input('Enter your move:\n') next_move = user_input.split(' ') user_action = next_move[0].title() # Set variables to generic item = 'item' direction = next_move[1].title() # Determines if a user is trying to move or get an item if user_action == 'Go': current_room = rooms[current_room][direction] msg = f"You traveled {direction}." else: msg = f"You can't go that way." # Picking up an item in room if user_action == 'Get': if item == rooms[current_room]['Item']: if item not in inventory: inventory.append(rooms[current_room]['Item']) msg = f"You have picked up the {item}" else: msg = f"You already have the {item}" else: msg = f"Can't find {item}" elif user_action == "Exit": break else: msg = f"Invalid Command"
核心错误分析
- 逻辑结构错误:用户输入处理代码在
while True循环之外,程序仅执行一次房间状态打印后就触发胜负判断退出,根本无法接收用户操作。 - 非法移动崩溃:执行
Go指令时直接通过字典索引获取房间,未检查方向是否合法,触发KeyError导致崩溃。 - 物品拾取失败:
item被固定赋值为'item',从未从用户输入中提取目标物品名称,永远无法匹配房间内的实际物品。 - 缩进错误:Boss判断语句后无缩进,导致胜负条件不受Boss房间约束,程序启动即触发失败逻辑。
修复后的代码
# Lists the rooms on the map rooms = { 'Cubicle': {'North': 'Copy Room', 'East': 'Coat Room', 'West': 'Break Room'}, 'Break Room': {'North': 'Restroom', 'East': 'Cubicle', 'Item': 'Backpack'}, 'Restroom': {'South': 'Break Room', 'Item': 'Hat'}, 'Copy Room': {'South': 'Cubicle', 'East': 'File Room', 'Item': 'Keys'}, 'File Room': {'West': 'Copy Room', 'South': 'Coat Room', 'East': 'Office', 'Item': 'Phone'}, 'Coat Room': {'West': 'Cubicle', 'North': 'File Room', 'Item': 'Coat'}, 'Office': {'West': 'File Room', 'South': 'Stairwell', 'Item': 'Keycard'}, 'Stairwell': {'North': 'Office', 'Villain': 'Boss'}, } # Keeps track of inventory inventory = [] # Keeps track of current room current_room = 'Cubicle' while True: # Print current room status print(f'You are in the {current_room}\nInventory : {inventory}\n{"-" * 27}') # Check and display nearby item if not collected if 'Item' in rooms[current_room] and rooms[current_room]['Item'] not in inventory: nearby_item = rooms[current_room]['Item'] # Handle plural/singular display if nearby_item[-1] == 's': print(f'You see {nearby_item}') else: print(f'You see a {nearby_item}') # Boss encounter check if 'Villain' in rooms[current_room] and rooms[current_room]['Villain'] == 'Boss': # Lose condition if len(inventory) < 6: print(f'You were caught by the Boss and now you will have to work all weekend') break # Win condition else: print(f'You were able to sneak by the boss with your hat pulled down low') print(f'and with the phone to your ear you looked too busy to interrupt') print(f'Congratulations you have escaped the office!') break # Get user input user_input = input('Enter your move:\n').strip() next_move = user_input.split() if not next_move: print("Please enter a valid command (e.g., 'Go North', 'Get Backpack', 'Exit')") continue user_action = next_move[0].title() msg = "" # Handle movement if user_action == 'Go': if len(next_move) < 2: msg = "Please specify a direction (e.g., 'Go North')" else: direction = next_move[1].title() if direction in rooms[current_room]: current_room = rooms[current_room][direction] msg = f"You traveled {direction}." else: msg = f"You can't go {direction} from here." # Handle item pickup elif user_action == 'Get': if len(next_move) < 2: msg = "Please specify an item to get (e.g., 'Get Backpack')" else: target_item = ' '.join(next_move[1:]).title() if 'Item' in rooms[current_room] and rooms[current_room]['Item'] == target_item: if target_item not in inventory: inventory.append(target_item) msg = f"You have picked up the {target_item}" else: msg = f"You already have the {target_item}" else: msg = f"Can't find {target_item} in this room." # Handle exit elif user_action == 'Exit': print("Exiting the game.") break # Invalid command else: msg = "Invalid Command. Valid commands: Go [Direction], Get [Item], Exit" # Print action result if msg: print(msg) print("") # Add blank line for readability
修复说明
- 调整循环结构:将用户输入处理移入循环内,保证程序持续响应用户操作。
- 修复缩进问题:将胜负条件嵌套在Boss房间判断内,只有进入Stairwell时才触发胜负逻辑。
- 避免非法移动崩溃:移动前检查方向是否存在于当前房间的可移动方向中,防止KeyError。
- 修复物品拾取:从用户输入中提取目标物品名称,与房间物品匹配,实现正确拾取。
- 增加输入校验:处理空输入、指令不完整的情况,提升程序健壮性。
- 优化物品显示:直接判断房间物品是否已拾取,简化逻辑。
内容的提问来源于stack exchange,提问作者Dan Ford
相关产品推荐
相关产品推荐

