PHPFixing
  • Privacy Policy
  • TOS
  • Ask Question
  • Contact Us
  • Home
  • PHP
  • Programming
  • SQL Injection
  • Web3.0

Sunday, August 14, 2022

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

 August 14, 2022     dictionary, key-value, nested, output, python     No comments   

Issue

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.



Answered By - Tim Roberts
Answer Checked By - Cary Denson (PHPFixing Admin)
  • Share This:  
  •  Facebook
  •  Twitter
  •  Stumble
  •  Digg
Newer Post Older Post Home

0 Comments:

Post a Comment

Note: Only a member of this blog may post a comment.

Total Pageviews

Featured Post

Why Learn PHP Programming

Why Learn PHP Programming A widely-used open source scripting language PHP is one of the most popular programming languages in the world. It...

Subscribe To

Posts
Atom
Posts
Comments
Atom
Comments

Copyright © PHPFixing