How do I replace a sub key in a nested dictionary, with a sub key from another nested dictionary, and also bringing the values?

Viewed 43

I am building a text rpg in tkinter. I have two dictionaries, one for inventory and one for all the items in the game. To access an items i usually type items['Dagger'] for instance.

I am trying to get my inventory to update when I get a new item. The inventory looks like this:

inventory = {
    'slot1': {
    'Empty': {
        'name': "empty",
        'icon': "images/icons/emptyslot.png",
        'equippable': False,
        'color': "black"
        },
    },
    'slot2': {
    'Empty': {
        'name': "empty",
        'icon': "images/icons/emptyslot.png",
        'equippable': False,
        'color': "black"
        },
    }
}

It has two slots right now, both are assigned the item "empty".

And this is my items dictionary:

items = {
#ONE HANDED WEAPONS
    'Dirk': {
        'name': f'Dirk',
        'mindamage': 1,
        'maxdamage': 5,
        'blockvalue': 0,
        'agility': 2,
        'strength': 0,
        'intelligence': 0,
        'coins': 20,
        'color': "gray",
        'icon': "images/icons/dirk.png",
        'equippable': True,
        'gearslot': "both"
        },

    'Empty': {
        'name': "empty",
        'icon': "images/icons/emptyslot.png",
        'equippable': False,
        'color': "black"
        },
}

If I want to replace an empty item with an actual item, i do this:

inventory['slot1'] = items['Dirk']

However, this does not bring in the item "key", only the values, and assigns it to ['slot1'].

I printed the dicts before and after assigning the item to the inventory; Before assigning:

{'Empty': {'name': 'empty', 'icon': 'images/icons/emptyslot.png', 'equippable': False, 'color': 'black'}}

After assigning:

{'name': 'Dirk', 'mindamage': 1, 'maxdamage': 5, 'blockvalue': 0, 'agility': 2, 'strength': 0, 'intelligence': 0, 'coins': 20, 'color': 'gray', 'icon': 'images/icons/dirk.png', 'equippable': True, 'gearslot': 'both'}

As you can see the "Dirk" key did not follow along, instead it just added all the values to ['slot1'], and this is causing some massive issues for my systems.

Does anyone know how to solve this?

0 Answers
Related