Python文本游戏Get Item指令无法添加物品至Inventory问题求助
问题分析与修复方案
核心问题及修复点:
列表append方法调用错误
原代码中收集物品时使用了inventory.append[current_room]["Item"],这是语法错误——append是列表的方法,必须用括号调用而非方括号。正确写法应为:inventory.append(rooms[current_room]["Item"])这个错误被
try-except块捕获,导致你看到的只是"Invalid Input"提示,而非实际错误信息。Armory房间物品键名不一致
所有房间的物品键都是'Item',但Armory房间写成了'Items'(复数),导致该房间的Ray Gun无法被检测和收集,永远凑不齐6个物品。修改为:'Armory': {'South': 'Med Bay', 'Item': 'Ray Gun'},错误提示逻辑不合理
当物品已在背包中时,原代码输出"Invalid Input"会误导用户,应改为提示物品已拥有:else: print(f"You already have the {item}!")冗余的异常捕获
原代码的try-except捕获了所有异常,掩盖了真实问题。建议缩小捕获范围(比如只捕获KeyError),或在调试阶段暂时移除,方便定位问题。
修正后的完整代码:
# Displays instructions def show_instructions(): print('Welcome to the Alien Invasion game') print('Aliens have invaded your ship!!') print('Collect all 6 items in order to defeat them!\n') print('Movement: go [direction](North, South, East, or West)') print('Get item: get [item name]') print('_____________________________________________________') # Rooms rooms = { 'Cockpit': {'West': 'Engine Control Room'}, 'Engine Control Room': {'East': 'Cockpit', 'North': 'Canteen', 'Item': 'Flashlight'}, 'Canteen': {'North': 'Sleeping Quarters', 'South': 'Engine Control Room', 'East': 'Med Bay', 'West': 'Command Center', 'Item': 'Energy Bar'}, 'Command Center': {'East': 'Canteen', 'Boss': 'Aliens'}, 'Sleeping Quarters': {'South': 'Canteen', 'East': 'Storage', 'Item': 'Space Suit'}, 'Armory': {'South': 'Med Bay', 'Item': 'Ray Gun'}, 'Storage': {'West': 'Sleeping Quarters', 'Item': 'Space Helmet'}, 'Med Bay': {'North': 'Armory', 'West': 'Canteen', 'Item': 'Stim Pack'}, } # List for players inventory inventory = [] # Tracks current room current_room = 'Cockpit' # Calls function show_instructions() # Loop to keep game going while True: # Prints Status print(f"You are in the {current_room}\nInventory: {inventory}\n") # Item Detection if 'Item' in rooms[current_room]: nearby_item = rooms[current_room]['Item'] if nearby_item not in inventory: print(f'There is a {nearby_item}') # Boss Detection if "Boss" in rooms[current_room]: # Lose Condition if len(inventory) < 6: print('ZAP ZAP ZAP\n YOU LOSE!!!') break # Win Condition else: print('CONGRATULATIONS!!\n YOU WIN!!') break # Player Input user_input = input('Enter your move: ') # Splitting Input next_move = user_input.split(' ') # Splitting to check whether input is a direction or get item action = next_move[0].title() item = None direction = None if len(next_move) > 1: if action == "Get": item = ' '.join(next_move[1:]).title() elif action == "Go": direction = next_move[1].title() # Movement if action == "Go": if not direction: print("Invalid Input: Specify a direction.") continue try: current_room = rooms[current_room][direction] print(f"You went {direction}.") print('_________________') except KeyError: print("You can't go that way.") # Get Items elif action == "Get": if not item: print("Invalid Input: Specify an item name.") continue try: room_item = rooms[current_room]["Item"] if item == room_item: if item not in inventory: inventory.append(room_item) print(f"You got {item}") else: print(f"You already have the {item}!") else: print(f"There is no {item} here.") except KeyError: print("There are no items to collect here.") # Exit Game elif action == "Exit": print("Thanks for playing!") break else: print("Invalid Input")
内容的提问来源于stack exchange,提问作者Charglie
相关产品推荐
相关产品推荐

