How to show specific item in one room?

Viewed 59

I'm taking a scripting class and we are working in python. My final project is a text based game and I'm having trouble figuring out how I can make only one item show up, depending on which room the user is in, out of a list of items. I've made a function to allow the user to add the item to the inventory, but I can only make 1 item show up in each room, or all items show up in each room.

rooms = {
    'Your bedroom': {
        'north': 'Alley Way'
    },
    'Alley Way': {
        'north': 'Main Hall',
        'east': 'Bar',
        'west': 'Ammo Store',
        'south': 'Your bedroom',
        'item': 'Cloaking Device'
    }, 
    'Bar': {
        'west': 'Alley Way'
    },
    'Ammo Store': {
        'east': 'Alley Way',
        'item': 'Gun'
    },        
    'Main Hall': {
        'north': 'Oval Office',
        'west': 'West Wing',
        'east': 'East Wing',
        'south': 'Alley Way'
    },         
    'East Wing': {
        'west': 'Main Hall'
    },         
    'West Wing': {
        'east': 'Main Hall'
    },         
    'Oval Office': {
        'south': 'Main Hall'
    }     
}

items = ['Cloaking Device', 'Whiskey', 'Gun', 'Noise Maker', 'Key', 'Rocket Launcher']

def player_stat():
    print("-" * 20)
    print('You are in the {}'.format(location))
    print('You have {} in your inventory.'.format(inventory))
    print("-" * 20)

def get_item():
    command = input('Would you like to pick up the {}?').format(items).strip().lower()
    if command == 'yes':
        inventory.append(items)
        print('{} has been added to your inventory'.format(items))

location = 'Your bedroom'
item = 'item'
inventory = []
direction = ''
while direction != 'exit':
    if location == 'Your bedroom':
        print('\nYou are currently in', location)

    possible_moves = rooms[location].keys()
    print('You can move:', *possible_moves)

    direction = input('What will yor next move be? ').strip().lower()
    print('You entered:', direction)

    if direction in rooms[location]:
        location = rooms[location][direction]
        if location == 'Alley Way':
            player_stat()
            print('You have left your room and entered the Alley Way.', '\n'
                  'You notice some alien guards roughing up a homeless man.', '\n'
                  'While wrangling the homeless man, you notice one of the guards drop their cloaking device.')
            if item:
                print('You see a {}'.format(items[0]))
            get_item()

input('Would you like to pick up the {}?').format(items) is where I am trying to select only the item that should be in each room, but I just can't seem to figure out how.

1 Answers

If I understand correctly, there is exactly one item in each room. As it is now, you don't have any connection between your items and the rooms they are in, so how would you know which one to show?

You could add each item to the rooms-dict for the related location, so you can access it by è.g. item = rooms[location]["item"] with the location you're currently in (exact syntax may be different depending on how your rooms-dict is structured). You could then pass the item as an input to the get_item-function, that then does the rest.


On an unrelated note, maybe you'd be interested in f-strings, if you're using python-3.6 or later. They make the string-formatting more readable, especially if you have multiple variables:

print(f'You have {inventory} in your inventory.')
vs.
print('You have {} in your inventory.'.format(inventory))
Related