Home > database >  How to print only one item from a dictionary with a changeable variable
How to print only one item from a dictionary with a changeable variable

Time:12-24

I am trying to print only one key and value from my dictionary at a time and have it print based on the player current room position. Every solution I tried so far, either prints all of the items, all the items 10 times (the number of how many items there are), or none. I been searching for web for several hours now, any help would be much appreciated!

rooms = {
    'Lower Deck': {'up': 'Main Deck'},
    'Main Deck': {'down': 'Lower Deck', 'forward': 'Main Hallway', 'item': 'Pistol'},
    'Main Hallway': {'right': 'Crew Cabins', 'back': 'Main Deck', 'forward': 'Air Lock'},
    'Crew Cabins': {'back': 'Main Hallway', 'item': 'Dog tags'},
    'Air Lock': {'back': 'Main Hallway', 'forward': 'Crews Lounge'},
    'Crews Lounge': {'back': 'Air Lock', 'right': 'Escape pods', 'left': 'Engineering Room', 'up': 'Operation Room', 'item': 'Grenade'},
    'Escape pods': {'back': 'Crews Lounge'},
    'Engineering Room': {'back': 'Crews Lounge'},
    'Operation Room': {'back': 'Crews Lounge', 'forward': 'Control Center'},
    'Control Center': {'back': 'Operation Room'}
}

This is the best result out of everything I tried so far

for i in rooms:
    i = current_room
    print('The current exit to the room are: {}'.format(rooms[i]))

# output:
The current exit to the room are: {'up': 'Main Deck'}
The current exit to the room are: {'up': 'Main Deck'}
The current exit to the room are: {'up': 'Main Deck'}
...

It will out put a line for the total number of items there are. That is only the first problem, the next problem I have to have it adjust based on what room the player is in. This is what I have currently have for players to move from room to room (this works):

current_room = 'Lower Deck'
command = input("Please enter your move:\n").split()
if command[0].capitalize() == 'Go':
    if command[1] in directions:
        room_dict = rooms[current_room]
        if command[1] in room_dict:
            current_room = room_dict[command[1]]
        else:
            print("There is no exit that direction.\n")

CodePudding user response:

If you just want to print the exits for the current room, you don't need to loop over rooms at all.

This will print the exits and where they lead:

print(rooms[current_room])

Or if you just want to print the exits:

for exit in rooms[current_room]:
    print(exit)
  • Related