Search code examples
pythondictionarynestedoutputkey-value

How to output one value from a dictionary whose key has two values


I'm working on a text-based game project for school and I'm trying to pull one value from my dictionary. The value I'm trying to pull is "gun" and not both "Lobby" and "gun". The output is: "You see: Lobby Gun". The output I want is: "You see: Gun". Is that possible? Thank you.

rooms = {
        'Lobby': {'North': 'Teller Room', 'South': 'Vestibule', 'East': 'Office 1', 'West': 'Office 2'},
         'Vestibule': {'North': 'Lobby'},
        'Office 2': {'East': 'Lobby', 'Item': 'Gun'},
         'Office 1': {'North': 'Bathroom', 'Item': 'Gloves'},
         'Bathroom': {'West': 'Lobby', 'Item': 'Keys'},
         'Teller Room': {'South': 'Lobby', 'East': 'Utility Room', 'West': 'Vault', 'Item': 'Bags'},
        'Utility Room': {'West': 'Teller Room', 'Item': 'Knife'},
         'Vault': {'East': 'Teller Room', 'Item': 'Money'}

}


   collection = rooms['Office 2'].values()
   print('You see: ', *collection)

Solution

  • If all you want to print is the item, then you should just print the item:

    collection = rooms['Office 2']['Item']
    

    Since not all rooms have an item, you'll need to check that.