为何Python龙文本游戏无法实现房间移动与退出功能?
问题排查:Python龙文本游戏无法移动或退出
问题概述
开发的Python龙文本游戏预期实现房间间移动、随时退出功能,但实际运行时,输入方向指令(如south)或exit均提示无效操作,无法切换房间或退出,游戏陷入循环。
问题代码
def show_instructions(): if __name__ == "__main__": while True: print('Dragon Text Game\n') print('\t\t\tDragon Text Game.') print('--------------------------------------------------------------------------------------- ------') print('Move commands: South, North, East, West') print('Type "Exit" to end the game.') print("Let's start with your name: ") name = input() print(f'Good luck, {name}!') # 简化版龙文本游戏的房间字典,关联房间之间的连接关系 rooms = { 'Great Hall': {'name': 'Great Hall', 'South': 'Bedroom', 'text': 'You are in the Great Hall.'}, 'Bedroom': {'name': 'Bedroom', 'North': 'Great Hall', 'East': 'Cellar', 'text': 'You are in the Bedroom.'}, 'Cellar': {'name': 'Cellar', 'West': 'Bedroom', 'text': 'You are in the Cellar. Type "Exit" to end the game!'} } directions = ['North', 'South', 'East', 'West'] current_room = rooms['Great Hall'] exit = False # 游戏主循环 while True: print(current_room['text']) command = input('\nEnter your move: ') if command == 'Exit': if current_room['name'] == 'Cellar': # 进入地窖则游戏完成 print('Congratulations !!') print("You've completed 'Dragon Text Game'.") print('Thanks for playing!') break if command in directions: if command in current_room: current_room = rooms[current_room[command]] else: print(f'No door in {command} direction!') else: print('Invalid move!')
运行输出
Dragon Text Game. --------------------------------------------------------------------------------------- ------ Move commands: South, North, East, West Type "Exit" to end the game. Let's start with your name: Reb Good luck, Reb! You are in the Great Hall. Enter your move: south Invalid move! You are in the Great Hall. Enter your move: east Invalid move! You are in the Great Hall. Enter your move: exit Invalid move! You are in the Great Hall.
问题原因
- 大小写不匹配:
- 代码中退出判断条件为
command == 'Exit',但用户输入的是小写exit,两者不匹配,无法触发退出逻辑。 - 方向列表
directions中的元素是首字母大写格式(如'South'),但用户输入的是小写south,导致command in directions判断不成立,无法触发房间移动逻辑。
- 代码中退出判断条件为
- 缩进错误:原代码中
else语句的缩进与对应if不匹配(多一层缩进),导致语法逻辑错误,即使输入符合条件的命令也无法正确执行。
修复方案
方案1:统一命令为为首字母大写(推荐)
将用户输入的命令转换为首字母大写格式,消除大小写差异,同时修正缩进:
# 游戏主循环 while True: print(current_room['text']) # 去除输入首尾空格并转换为首字母大写 command = input('\nEnter your move: ').strip().capitalize() if command == 'Exit': if current_room['name'] == 'Cellar': print('Congratulations !!') print("You've completed 'Dragon Text Game'.") print('Thanks for playing!') break if command in directions: if command in current_room: current_room = rooms[current_room[command]] else: print(f'No door in {command} direction!') else: print('Invalid move!')
方案2:支持任意大小写输入
如果希望兼容用户任意大小写的输入习惯,可以将命令和判断条件统一转为小写:
# 游戏主循环 while True: print(current_room['text']) # 去除输入首尾空格并转换为小写 command = input('\nEnter your move: ').strip().lower() if command == 'exit': if current_room['name'] == 'Cellar': print('Congratulations !!') print("You've completed 'Dragon Text Game'.") print('Thanks for playing!') break # 将方向列表转为小写后匹配 if command in [d.lower() for d in directions]: # 找到对应的原方向(首字母大写) direction = next(d for d in directions if d.lower() == command) if direction in current_room: current_room = rooms[current_room[direction]] else: print(f'No door in {direction} direction!') else: print('Invalid move!')
内容的提问来源于stack exchange,提问作者Rebisnotagoodcoder
相关产品推荐
相关产品推荐

