游戏开发问题:使用.lower()统一输入大小写导致游戏运行异常
文字冒险游戏输入大小写统一问题排查与修复
问题根源
- 移动逻辑失效:启用
direction = direction.lower()后,原移动判断条件(如direction == 'go North')是混合大小写,与转小写后的输入(如go north)完全不匹配,导致移动功能无法触发。 - 退出逻辑失效:退出判断
direction.lower() == 'Exit'中,右侧字符串是首字母大写,而左侧是全小写,两者永远不相等,无法触发退出。 - 拾取逻辑失效:物品拾取判断
direction == 'get ' + item未做大小写处理,当用户输入小写指令(如get laptop)时,与get Laptop不匹配,无法拾取物品。 - 变量未定义报错:当用户不输入移动指令时,
new_location变量未被赋值,后续判断new_location == 'Basement'会抛出NameError,直接终止游戏。 - 初始位置未初始化:代码中未定义初始
location变量,游戏启动时会直接报错(虽然你提到起始在galley,但代码里没做初始化)。
修复方案
- 统一将用户输入转为小写,再根据指令类型处理格式(比如提取方向时转为首字母大写,匹配rooms字典中的键)。
- 所有判断逻辑基于转小写后的输入,或使用不区分大小写的比较方式。
- 在循环开始时初始化
new_location为当前位置,避免未定义报错。 - 初始化
location为起始房间(如'Galley')。
修正后的完整代码
# 补充缺失的rooms字典示例结构 rooms = { 'Galley': { 'North': 'Kitchen', 'Item': 'Apple' }, 'Kitchen': { 'South': 'Galley', 'East': 'Basement', 'Item': 'Laptop' }, 'Basement': { 'West': 'Kitchen', 'Item': 'Mouse' } # 其他房间请自行补充完整 } def rooms_movement(location, direction): new_location = location if location in rooms: if direction in rooms[location]: new_location = rooms[location][direction] return new_location def get_item(location): return rooms[location]['Item'] def game_play(): print('Welcome to the "COOLEST GAME!" ') print('The beginning of this epic journey starts in the galley.') print('To move between rooms type go North, go South, go East, and go West.') print('You must acquire a total of 8 separate items.') print("Add to Inventory: get 'item name'\n") game_play() Inventory = [] items = ['Laptop', 'Mouse', 'Apple', 'Underwear', 'Puppy', 'Microchip', 'Cell Phone', 'Coded Instructions'] # 初始化起始位置 location = 'Galley' while True: print("The room you're in is the: ", location) print('Inventory: ', Inventory) item = get_item(location) print('You see a ', item) print('--------------------') direction = input("Type the direction you would like to move or type exit or get item: ") # 统一转为小写处理,避免重复调用.lower() direction_lower = direction.lower() print('----------------------------\n') # 退出逻辑:匹配小写的'exit' if direction_lower == 'exit': print(' * * ') print('******') print(' v "Bye!!!"') exit(0) # 初始化new_location为当前位置,避免未定义报错 new_location = location # 移动逻辑:通过前缀判断移动指令,不区分大小写 if direction_lower.startswith('go '): # 提取方向并转为首字母大写,匹配rooms字典中的键格式 move_dir = direction_lower[3:].strip().capitalize() new_location = rooms_movement(location, move_dir) if new_location == location: print("Error, there is a wall... ouch!\n") print('----------------------------\n') else: location = new_location # 拾取逻辑:统一转为小写后匹配,支持任意大小写输入 if direction_lower == f'get {item.lower()}': if item in Inventory: print('Item already taken, go to another room!!') else: Inventory.append(item) # 胜利判断 if len(Inventory) == 8: print('Congratulations! You have collected all items and defeated the Robot') print(' * * ') print('******') print(' v "Bye!!!"') exit(0) # 失败判断:确保new_location始终有定义 elif len(Inventory) != 8 and new_location == 'Basement': print("You haven't collected all of the items before entering the basement!! \n") print("Game Over") print("Please restart and try again") exit(0)
关键修复点说明
- 新增
direction_lower变量统一存储小写输入,所有判断基于该变量,避免重复调用.lower()。 - 移动指令通过
startswith('go ')判断,提取方向后转为capitalize()格式,匹配rooms字典中的方向键。 - 拾取指令通过
f'get {item.lower()}'生成小写的目标字符串,和用户输入的小写指令匹配,支持任意大小写的输入格式。 - 循环开始时初始化
new_location = location,确保任何情况下该变量都有值,避免未定义报错。 - 补充了
rooms字典的示例结构(原代码缺失),并初始化location为'Galley',保证游戏能正常启动。
内容的提问来源于stack exchange,提问作者christopher elliott
相关产品推荐
相关产品推荐

