【问题标题】:Move 'item' from one dictionary to a new list将“项目”从一个字典移动到一个新列表
【发布时间】:2021-12-04 13:51:17
【问题描述】:

我一直在编写以下代码,但似乎无法将我的项目从字典移动到库存列表。我一定在某个地方有错误,我很难找到它。有人可以帮我吗?我认为我的问题出在哪里:
如果命令 == “获取项目”: 如果房间里有“物品”: 库存.追加(房间[当前房间]['项目']) 删除房间[current_room]['item'] print('库存物品:. {}.'.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))

【问题讨论】:

  • 请正确格式化您的帖子并正确缩进您的代码,感谢您帮助帮助您。

标签: python list dictionary


【解决方案1】:
inventory.append(rooms[current_room]['item name'])
# not 
inventory.append(rooms[current_room]['item'])

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2014-04-06
    • 1970-01-01
    • 2020-12-30
    相关资源
    最近更新 更多