1

I'm working on a game and one of the commands, one to pickup items, is not working. The code pretty much checks to see if the item is in the room and if it is then to copy that to the players inventory and then delete the item from the room. However no matter what I try it either does nothing or copies the keys inside to the dictionary.

Here's the code:

def pickup(self, item):
    conf = input('Do you want to pick up the ' + item.lower() + ': ')
    if conf.lower() == 'y' or 'yes':
        try:
            self.inventory.update(room[self.room_number]['items'][item])
            del room[self.room_number]['items'][item]
        except KeyError:
            print('You look everywhere but can\'t find a ' + item.lower())
    else:
        print('You consider it, but decide not to pick up the ' + item.lower())

When I print the inventory dictionary I get this

player.inventory
    {
     'type': 'weapon',
     'equippable': True, 
     'value': 0, 
     'desc': 'a wooden stick, could be useful', 
     'name': 'Wooden Stick', 
     'perks': {
         'defense': 0, 
         'health': 0, 
         'damage': 6, 
         'magic_damage': 0
     }
}

or

{}

What I want is this:

player.inventory
    {
    'wooden stick':{
        'type': 'weapon', 
        'equippable': True, 
        'value': 0, 
        'desc': 'a wooden stick, could be useful', 
        'name': 'Wooden Stick', 
        'perks': {
            'defense': 0, 
            'health': 0, 
            'damage': 6, 
            'magic_damage': 0
            }
        }

Does anybody know how I can get this result. Nothing I try seems to work and I have checked to see whether anyone has answered this but can't find anything on it.

Thanks :)

SuperBiasedMan
  • 9,814
  • 10
  • 45
  • 73

2 Answers2

2

You're using the wrong function. In this line:

self.inventory.update(room[self.room_number]['items'][item])

update will essentially add all the keys and values from room[self.room_number]['items'][item] to self.inventory.

Instead, you want to assign the item dictionary as a value in the inventory dictionary, with a key for that item.

So you should do this:

self.inventory[item] = room[self.room_number]['items'][item]

Or better yet, as @MKesper points out, you should pop the key so that it's removed from the room's items dictionary when you put it into the inventory:

self.inventory[item] = room[self.room_number]['items'].pop(item, None)

This will attempt to get item out of the dictionary, and if it's not found None will be returned instead. If you remove None you'll instead have a KeyError, and depending on how confident you are in item being a valid key, a KeyError might be better to catch the unusual cases when incorrect keynames have appeared.

Docs on dict.pop

SuperBiasedMan
  • 9,814
  • 10
  • 45
  • 73
  • This seems to work. Thought I had tried that but had written self.inventory = room[self.room_number]['items'][item]. Should've worked that out. Thanks for the help. – MicroTransactionsMatterToo Nov 11 '15 at 09:23
  • 1
    Additionally it's probably better to "pop" the key out of the dict: my_dict.pop("key", None) as written at http://stackoverflow.com/questions/11277432/how-to-remove-a-key-from-a-dictionary – MKesper Nov 11 '15 at 09:24
  • Thanks, had read that but wasn't sure what it did. Reread it and it makes more sense now. :) – MicroTransactionsMatterToo Nov 11 '15 at 09:26
0

You can use update method:

dict1.update(dict2)

jalanga
  • 1,456
  • 2
  • 17
  • 44