文本游戏中While循环内If条件无法终止循环的问题求助
问题修复方案
先梳理你代码里的核心问题,再给出修正后的完整实现:
核心问题点
- 目标物品列表不全:你定义的物品列表只包含5个物品,漏掉了
Religious Shrine里的Dagger Sheath,永远凑不齐要求的6个 - 女王密室无进入校验:当前代码允许玩家直接进入最终房间,没有检查物品收集情况
- 数量判断逻辑错误:
len(inventory) == '6'是字符串和整数的无效比较,永远不会成立 - 拾取命令匹配过严:必须严格输入
Take [物品全称](含大小写),玩家输入稍有偏差就失效
修正后的完整代码
rooms = {'Jail Cell': {'name': 'Jail Cell', 'South': 'Barracks', 'text': 'You\'re in the Jail Cell.', 'item': 'whole lot of nothing'}, 'Barracks': {'name': 'the Barracks', 'West': 'Dining Hall', 'North': 'Jail Cell', 'text': 'You\'re in the Barracks.', 'item': 'Strange Potion'}, 'Dining Hall': {'name': 'the Dining Hall', 'West': 'Queens Chamber', 'North': 'Alchemist Room', 'South': 'Bedchamber', 'East': 'Barracks', 'text': 'You\'re in the Dining hall.', 'item': 'Stained Wine Glass'}, 'Queens Chamber': {'name': 'the Queens Chamber', 'East': 'Dining Hall', 'text': 'You\'re in the Queen\'s Chamber.', 'item': 'nothing'}, 'Bedchamber': {'name': 'the Bedchamber', 'East': 'Religious Shrine', 'North': 'Dining Hall', 'text': 'You\'re in the Bedchamber.', 'item': 'Rumor Book'}, 'Religious Shrine': {'name': 'Religious Shrine', 'West': 'Bedchamber', 'text': 'You\'re in the Religious Shrine.', 'item': 'Dagger Sheath'}, 'Alchemist Room': {'name': 'Alchemist Room', 'East': 'Commanders Chamber', 'South': 'Dining Hall', 'text': 'You\'re in the Alchemist\'s Room.', 'item': 'Potion Recipe'}, 'Commanders Chamber': {'name': 'Commanders Chamber', 'West': 'Alchemist Room', 'text': 'You\'re in the Commander\'s Chamber.', 'item': 'Queen\'s Chamber Key'} } directions = ['North', 'South', 'East', 'West'] current_room = rooms['Jail Cell'] inventory = [] # 补全6个必须收集的物品 required_items = ['Strange Potion', 'Stained Wine Glass', 'Rumor Book', 'Dagger Sheath', 'Potion Recipe', 'Queen\'s Chamber Key'] # 游戏初始菜单 def menu(): print('The Secret of the Forgotten Queen: A Text Based Adventure') print('To move, type: North, South, East, or West.') print('To search a room, type "Search Room".') print('To pick up an item, type: "Take" followed by the item name.') print('Collect all 6 items to discover what has happened to the Queen.') print('\nYou awaken in an old Jail Cell. An apparition appears! It\'s a beautiful lady who explains that you must ' 'find the secrets of this castle so that the Queen may finally rest.') menu() # 游戏主循环 while True: # 非最终房间时显示当前状态 if current_room['name'] != 'Queens Chamber': print(f'You\'re in the {current_room["name"]}.') print('Inventory:', inventory) command = input('\nWhat\'s your next move Hero?').strip() # 处理移动逻辑,重点拦截最终房间的进入请求 if command in directions: # 检测是否要进入女王密室 if command in current_room and current_room[command] == 'Queens Chamber': # 检查是否收集全所有必备物品 if all(item in inventory for item in required_items): current_room = rooms['Queens Chamber'] else: print('The door to the Queen\'s Chamber is sealed! You need to collect all 6 items first.') else: # 其他正常移动逻辑 if command in current_room: current_room = rooms[current_room[command]] else: print('Nothing found in that direction. Which direction now?') # 处理拾取物品,支持大小写容错 elif command.lower().startswith('take '): taken_item = command[5:].strip() room_item = current_room['item'].lower() if taken_item.lower() == room_item: # 只拾取有效物品,排除无意义的"nothing"类 if current_room['item'] not in inventory and current_room['item'] not in ['whole lot of nothing', 'nothing']: inventory.append(current_room['item']) print(f'You picked up the {current_room["item"]}!') else: print('\nYou have already picked this item up, or there\'s nothing to take here!\n') else: print('That item isn\'t in this room!') elif command == 'Search Room': print(f'You find a {current_room["item"]}.') # 退出游戏 elif command.lower() == 'quit': print('See ya next time!') break # 触发结局并终止循环 if current_room['name'] == 'Queens Chamber': print('The Queen and her lover the Commander of the Knights. Another unfortunate Romeo and Juliet scenario. What\'s the moral of this story?') print('You win!') break # 无效命令提示 else: print('Invalid input. Review the instructions!')
关键修改说明
- 补全必备物品列表:加入
Dagger Sheath,确保是要求的6个物品 - 新增最终房间校验:玩家试图进入女王密室时,用
all()检查是否收集全所有物品,未收集全则阻止进入并提示 - 优化拾取逻辑:支持大小写容错,拆分命令提取物品名,避免严格匹配导致的无效操作
- 修复结局触发:只有成功进入最终房间后才触发结局并终止循环,符合游戏逻辑
内容的提问来源于stack exchange,提问作者BobGnarley43
相关产品推荐
相关产品推荐

