首页 > 解决方案 > 将“项目”从一个字典移动到一个新列表

问题描述

我一直在处理以下代码,但似乎无法让我的项目从字典移动到库存列表。我一定在某个地方有错误,我很难找到它。有人可以帮我吗?我认为我的问题出在哪里:
if command == "get item": if 'item' in rooms: inventory.append(rooms[current_room]['item']) del rooms[current_room]['item'] print ('Items in Inventory:. {}.'.format(inventory)) 我是 Python 新手,只需要一点帮助来了解它的格式化方式。提前致谢。

      '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']
get_items = ['get item']
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!!!')
        print('Congratulations! You have killed the shark! Way to go!')

    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?')
    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 == "get item":
        if 'item' in rooms:
            inventory.append(rooms[current_room]['item'])
            del rooms[current_room]['item']
        print('Items in Inventory:. {}.'.format(inventory))

标签: pythonlistdictionary

解决方案


inventory.append(rooms[current_room]['item name'])
# not 
inventory.append(rooms[current_room]['item'])

推荐阅读