Python将字典中物品转移到列表功能失效问题求助
问题诊断与代码修复
你的代码存在几处语法和逻辑错误,直接导致物品拾取功能失效,具体问题和修复方法如下:
- 语法错误:开头游戏说明缺失print语句
你开头的三行游戏说明字符串没有被print()包裹,末尾还多了一个多余的右括号,运行时会直接报错。 - 核心逻辑错误1:物品存在性判断错误
你写的if 'item' in rooms:判断的是「所有房间的总字典里有没有叫'item'的键」,显然rooms的键都是房间名(Hull、Bow等),这个判断永远为False,拾取逻辑永远不会执行。应该判断当前房间是否存在可拾取的物品。 - 核心逻辑错误2:当前房间取值方式错误
代码里current_room直接存储的是当前房间的字典对象,不是房间名称,你写的rooms[current_room]['item']会直接报错,因为字典的key不能是另一个字典,直接用current_room['item']就能拿到当前房间的物品。 - 逻辑不匹配:输入规则和判断规则不一致
你游戏提示要求用户输入get *物品名*,但代码只判断command == "get item",用户按照提示输入根本触发不了拾取逻辑。 - 隐藏报错风险:删除item键后后续访问异常
你拾取物品后直接del rooms[current_room]['item'],后续再进入该房间打印物品信息时,不存在item键会直接抛出KeyError,建议不要删除键,而是把物品值设为空来标记已拾取。 - 玩法逻辑缺失:没有判断集齐物品才可以击败鲨鱼
当前只要进入Lido Deck就直接提示通关,没有校验是否集齐6件物品,不符合初始的玩法设定。
修复后完整代码
# 游戏说明 print('Directions: To move around the ship: type a go command in Nautical Terms such as "go forward, go aft,' ' go starboard, or go port". To put an item into your inventory: type "get *name of item*" Remember, ' 'you will need to collect all six items before you get to the shark or you will be shark bait.\n' 'If you would like to end the game, type exit.\n\n') rooms = {'Hull': {'name': 'the Hull', 'go forward': 'Bow', 'go aft': 'Stern', 'go starboard': 'Cabin B', 'go port': 'Galley', 'item': ' ', 'item name': ' ', 'text': 'You are in the Hull.'}, #starting point, no items 'Bow': {'name': 'the Bow', 'go port': 'Bridge', 'go aft': 'Hull', 'item': 'Chum Bucket', 'item name': 'a Chum Bucket', 'text': 'You are in the Bow.'}, 'Bridge': {'name': 'the Bridge', 'go starboard': 'Bow', 'item': 'Oxygen Tank', 'item name': 'an Oxygen Tank', 'text': 'You are in the Bridge.'}, 'Stern': {'name': 'the Stern', 'go forward': 'Hull', 'go port': 'Cabin A', 'item': 'Rope', 'item name': 'a Rope', 'text': 'You are in the Stern'}, 'Cabin A': {'name': 'Cabin A', 'go starboard': 'Stern', 'item': 'Spearhead', 'item name': 'a Spearhead', 'text': 'You are in Cabin A'}, 'Cabin B': {'name': 'Cabin B', 'go port': 'Hull', 'item': 'Spear Gun', 'item name': 'a Spear Gun', 'text': 'You are in Cabin B'}, 'Galley': {'name': 'the Galley', 'go starboard': 'Hull', 'go aft': 'Lido Deck', 'item': 'Dive Knife', 'item name': 'a Dive Knife', 'text': 'You are in the Galley'}, 'Lido Deck': {'name': 'the Lido Deck', 'go forward': 'Galley', 'item': 'Shark', #villan 'item name': 'A SHARK!', 'text': 'You are at the Lido Deck'}, } directions = ['go aft', 'go forward', 'go port', 'go starboard'] current_room = rooms['Hull'] inventory = [] while True: if current_room['name'] == 'the Lido Deck': print('You are now on the Lido Deck and you see a SHARK!!!') if len(inventory) == 6: print('Congratulations! You have killed the shark! Way to go!') else: print('You did not collect all 6 items! You are shark bait. Game Over.') break print('You are in {}.'.format(current_room['name']), '\n', 'You have {}'.format(inventory), '\n', 'You see {}.'.format(current_room['item name'])) command = input('\nWhat would you like to do?').strip() # 退出逻辑 if command.lower() == 'exit': print('Game ended.') break # 移动逻辑 if command in directions: if command in current_room: current_room = rooms[current_room[command]] else: print('You have hit a wall. Try another direction.') # 拾取物品逻辑 if command.startswith('get '): target_item = command.split(maxsplit=1)[1] # 判断当前房间物品是否匹配 if current_room['item'].strip() and target_item == current_room['item']: inventory.append(current_room['item']) # 标记物品已拾取 current_room['item'] = ' ' current_room['item name'] = 'nothing' print(f'You got the {target_item}!') print('Items in Inventory: {}.'.format(inventory)) else: print(f'There is no {target_item} here!')
内容的提问来源于stack exchange,提问作者idgiesmom
相关产品推荐
相关产品推荐

