Python文本游戏输入校验问题:修复仅输Take即可添加物品的bug
问题分析
你的核心问题有两个:
- 命令匹配逻辑错误:你写的
('Take'+item) not in user_input是把Take和物品名直接拼接(比如TakeDragonNecklace),但用户实际输入是Take Dragon Necklace(带空格),所以这个判断永远不会触发。 - 代码执行顺序与条件覆盖问题:当前只要输入包含
Take,不管有没有物品名,都会执行添加物品的逻辑;而且无效输入的判断位置不对,被后续的Take处理逻辑覆盖了。
修复方案
步骤1:提前初始化item变量,避免未定义报错
原代码中如果当前房间是Terrace或Dragon Pit,item变量会未定义,导致后续判断报错。在循环开头先初始化:
# 在gameplay_loop的while True开头添加 item = None if current_room != 'Terrace' and current_room != 'Dragon Pit': item = rooms[current_room]['item']
步骤2:重构Take命令的判断逻辑
拆分用户输入,检查是否符合Take [物品名]的格式,并且物品名和当前房间的物品匹配:
- 先把用户输入按空格拆分,判断是否至少有两个部分,第一部分是
Take - 然后把拆分后的剩余部分拼接成物品名,和当前房间的物品对比
- 只有完全匹配时才添加到背包,否则输出无效输入
步骤3:调整条件判断的顺序
把无效输入的判断放在最后,确保所有合法命令都被处理后,再触发无效输入提示。
修改后的关键代码片段
def gameplay_loop(): move_direction = ['North', 'South', 'East', 'West'] inventory = [] current_room = 'Terrace' # Gameplay loop while True: item = None if current_room != 'Terrace' and current_room != 'Dragon Pit': item = rooms[current_room]['item'] # Display Inventory print('Inventory', inventory, '\n') # Shows current location and item if current_room == 'Terrace': print('You are on the {}.\n'.format(current_room)) elif current_room in ['Daenerys Chambers', 'Barristan\'s Chambers']: print('You are in {}.'.format(current_room)) if item and item not in inventory: print('Item:', item, '\n') else: print('You have collected all of the items in this room.\n') elif current_room != 'Dragon Pit': print('You are in the {}.'.format(current_room)) if current_room == 'Torture Cells': print('A sign reads: "Beware of Dragon Pit: East"') if item and item not in inventory: print('Item:', item, '\n') else: print('You have collected all of the items in this room.\n') else: # Dragon Pit print('You are in the Dragon Pit.\n') # Display user input user_input = input('Enter your move:' + '\n' + '> ') # Movement control print('') # Space for visual formatting user_input = user_input.strip() input_parts = user_input.capitalize().split(maxsplit=1) # 拆分命令和参数 print('<> '*50) # 处理移动命令 if user_input in move_direction: if user_input in rooms[current_room].keys(): current_room = rooms[current_room][user_input] print(current_room) print('<> '*50) else: print('You can\'t go there!') print('<> '*50) # 处理退出命令 elif user_input == 'Exit': print('Thank you for playing!') print('<> '*50) break # 处理Take命令 elif len(input_parts) >= 2 and input_parts[0] == 'Take': if current_room == 'Terrace': print('There is no item here!') print('<> '*50) elif current_room == 'Dragon Pit': print('There is no item to take here!') print('<> '*50) else: requested_item = input_parts[1] if requested_item == item: if item not in inventory: inventory.append(item) print('You found:', item) print('<> '*50) else: print('You already have this item!') print('<> '*50) else: print('Invalid input') print('<> '*50) # 处理其他无效命令 else: print('Invalid input') print('<> '*50) # Game over if len(inventory) == 6 and current_room == 'Dragon Pit': print('\nYou have defeated the Dragons and saved the Seven Kingdoms!') print('Thanks for playing!') restart_game() elif current_room == 'Dragon Pit': print('\nYou died.') print('Collect all six items before facing the Dragons...') restart_game() def restart_game(): # Restart or exit game restart = input('Play again? (y,n):\n> ') if restart.lower() == 'y': start_menu() gameplay_loop() elif restart.lower() == 'n': print('Goodbye.') sys.exit() else: print('Invalid input') restart_game()
修复说明
- 用
split(maxsplit=1)拆分命令,把Take和物品名分开,确保只有输入Take [正确物品名]才会添加物品 - 提前初始化
item变量,避免未定义的报错 - 调整了房间信息显示的逻辑,增加了对Dragon Pit的处理
- 把无效输入的判断放在最后,确保所有合法命令都被处理后才触发
内容的提问来源于stack exchange,提问作者Ascents
相关产品推荐
相关产品推荐

