weapons = {
'sword' : {
'damage' : 10,
'type_damage' : 'sharp',
'max_durability' : 20
},
'spear' : {
'damage' : 7,
'type_damage' : 'pointy',
'max_durability' : 25
},
'log' : {
'damage' : 5,
'type_damage' : 'blunt',
'max_durability' : 15
}
}
inventory = {
}
inventory.update(weapons['sword'])
print(inventory)
I want to also add the ‘sword’ to inventory, not just the arguments, but I can’t figure out how.
I want it to output
{'sword' : {'damage': 5, 'type_damage': 'blunt', 'max_durability': 15}}
but instead it gives
{'damage': 5, 'type_damage': 'blunt', 'max_durability': 15}
>Solution :
update function returns only the value of a key-value pair.
To assign the key as well, you can do it like this:
weapons = {
'sword' : {
'damage' : 10,
'type_damage' : 'sharp',
'max_durability' : 20
},
'spear' : {
'damage' : 7,
'type_damage' : 'pointy',
'max_durability' : 25
},
'log' : {
'damage' : 5,
'type_damage' : 'blunt',
'max_durability' : 15
}
}
inventory = {
}
inventory.update({'sword': weapons['sword']})
print(inventory)
Output:
{'sword': {'damage': 10, 'type_damage': 'sharp', 'max_durability': 20}}