Doubt with reals and dictionaries in Python

0

You see, I'm in Codecademy doing this exercise: link

And I can not think of any way to add 50 to the 'gold' value. My code is as follows:

inventario = {
'oro' : 500,
'morral' : ['piedra', 'soga', 'piedra preciosa'],
'mochila' : ['xilofon','cuchillo', 'bolsa de dormir','pan flauta'],
'bolsillo' : ['caracol', 'mora', 'lanas']
}

inventario['bolsa de arpillera'] = ['manzana', 'rubi chiquito', 'osito panda']

inventario['morral'].sort() 

# Aca va tu codigo
inventario['mochila'].sort()
inventario['mochila'].remove('cuchillo')
inventario['oro'].append(+50)
    
asked by Feror 12.07.2016 в 22:41
source

1 answer

2

The first thing you must take into account is that the value of the key oro not is a list, if not an integer, and consequently you can not use the append method, it is more , the append method in a list does not serve to modify / increase the numeric value of its elements, but it adds an element to it.

Then, you must consider that to modify an integer variable you have to make a reassignment, for example:

a = 10 # Valor inicial de a
# ...
a = a + 5 # Aumentando el valor de a en 5

The same would happen for the case of your dictionary:

inventario['oro'] = inventario['oro'] + 50

Or if you want to shorten a bit:

inventario['oro'] += 50

It will already depend on which way you get more readable .

    
answered by 12.07.2016 / 23:41
source