Home > Software design >  Move 'item' from one dictionary to a new list
Move 'item' from one dictionary to a new list

Time:10-18

I have been working on the following code and I can't seem to get my items to move from the dictionary to the inventory list. I must have an error somewhere and I am having trouble finding it. Can someone help me out? I think my problem is where it have:
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)) I am new to Python and just need a little bit of help with the way this might be formatted. Thanks in advance.

      '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))
<iframe name="sif1" sandbox="allow-forms allow-modals allow-scripts" frameborder="0"></iframe>

CodePudding user response:

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