Python文本类游戏Exit与拾取物品功能失效问题求助
问题修复方案
核心问题分析
你的代码里两个功能失效的根源都在输入处理的格式化逻辑:
- Exit指令失效:输入
exit后被capitalize()处理成Exit,而Exit在directions集合里,会进入第一个if command in directions分支,但所有房间的方向里都没有Exit,所以只会输出"You cannot go that way",永远到不了处理退出的elif分支。 - 拾取指令失效:
capitalize()只会把输入的第一个单词首字母大写,比如输入pick up Lever会被处理成Pick up lever,但你匹配的是pick up {current_item}(current_item是首字母大写的,比如Lever),字符串完全不匹配,所以触发不了拾取逻辑。
修复后的完整代码
def instructions(): print("Welcome to Grizom's Greed: A Dragon Adventure Game") print('--------------') print('To slay Grizom, collect all pieces of the legendary') print('crossbow and make your way to the cave below the castle.') print('If you enter the cave before you have reforged the weapon') print('you are sure to perish.') print('Use the cardinal directions - North, South, East, and West to') print('navigate the land and use "Pick up [item name]" to get item.') print('Enter "Exit" to quit.') print('--------------') rooms = { 'Drawbridge': {'South': 'Main Courtyard', 'East': 'Stables', 'Item': 'Lever'}, 'Main Courtyard': {'South': 'Training Grounds', 'North': 'Drawbridge', 'West': 'Serene Lake', 'East': 'Castle Main Hall', 'Item': 'Bolt'}, 'Stables': {'West': 'Drawbridge', 'Item': 'Trigger'}, 'Serene Lake': {'East': 'Main Courtyard', 'Item': 'Gear'}, 'Castle Main Hall': {'North': 'Music Room', 'West': 'Main Courtyard', 'Item': 'Bow'}, 'Music Room': {'South': 'Castle Main Hall', 'Item': 'Bow String'}, 'Training Grounds': {'North': 'Main Courtyard', 'East': 'Cave', 'Item': 'Stock'}, } # 只保留方向的小写形式,退出指令单独处理 directions: set[str] = {'north', 'east', 'west', 'south'} # user instructions instructions() # main loop def def main(): location = 'Drawbridge' inventory = [] def show_status(): print('') print(f'You are currently at the {location}') print(f'You currently have:', *inventory, sep='-') if rooms[location]['Item'] in inventory: print('This room is empty.') else: print(f'You see a ' + rooms[location]['Item']) while True: show_status() possible_moves = rooms.get(location, {}) current_item = rooms[location]['Item'] # 输入统一转小写,避免大小写匹配问题 command = input('\nPlease enter a direction or take an available item: \n').strip().lower() # 先处理退出指令 if command == 'exit': print('Thank you for playing') break # 处理方向移动 if command in directions: # 把小写方向转成首字母大写,匹配rooms里的键 command_cap = command.capitalize() if command_cap not in rooms[location]: print('You cannot go that way') elif command_cap in possible_moves: location = possible_moves.get(command_cap, location) if location == 'Cave': if len(inventory) != 7: print('Against all advice and reason, you have entered the cave without the crossbow.') print('You did not survive. Game Over') break else: print('With the legendary crossbow at your side,') print('You have slayed Grizom. Congratulations!') break # 处理拾取指令:拆分输入,判断前缀是否是'pick up' elif command.startswith('pick up '): # 提取物品名,转成首字母大写匹配rooms里的Item picked_item = command[len('pick up '):].strip().capitalize() if picked_item == current_item: if current_item not in inventory: print(f'You picked up the {current_item} and added it to your inventory.') inventory.append(current_item) else: print('You already have this item.') else: print(f'There is no {picked_item} here.') else: print('Invalid input') if __name__ == '__main__': main()
关键修复点
- 退出指令优先级调整:把
command == 'exit'的判断移到最前面,避免被方向分支拦截。 - 输入统一转小写:消除大小写差异带来的匹配问题,方向判断时再转成首字母大写匹配房间字典的键。
- 拾取指令改为前缀匹配:用
startswith('pick up ')判断指令类型,再提取物品名并格式化,不管用户输入的物品名大小写都能正确匹配。 - 简化directions集合:只保留小写方向,退出指令单独处理,避免逻辑混淆。
内容的提问来源于stack exchange,提问作者NellyW
相关产品推荐
相关产品推荐

