Add 50 to the number stored under the 'gold' key.. error


#1

Add 50 to the number stored under the 'gold' key..
And I did this

inventory = {
'gold' : 500,
'pouch' : ['flint', 'twine', 'gemstone'], # Assigned a new list to 'pouch' key
'backpack' : ['xylophone','dagger', 'bedroll','bread loaf']
}

Adding a key 'burlap bag' and assigning a list to it

inventory['burlap bag'] = ['apple', 'small ruby', 'three-toed sloth']

Sorting the list found under the key 'pouch'

inventory['pouch'].sort()
inventory['pocket']=['seashell','strange berry','lint']
inventory['backpack'].sort()
inventory['backpack'].remove('dagger')
inventory['gold']+50 # It shows error in this line but I m doing what the question asked


#2

Are you? Re-examine how addition works in python. What do you get if you write 4 + 5 on a line by itself? You're not storing the result in anything.


#3

i=inventory['gold']+50
print i

It is printing 550
But I still get an error according to which I m supposed to add 50 to the gold total. What does that meannow ?


#4

You're adding it to a variable, so that's a step in the right direction. But the value of inventory['gold'] is still 500. You want to be able to say print inventory['gold'] and get 550 printed out. So you have to store the value back into inventory['gold'].


#5

Hi try to add it like that

'gold': 500 + 50

Inside inventory


#6

Thanks! James
It worked :slightly_smiling:


#7

Thanks a lot.
It worked :slightly_smiling:


#8

Try this inventory['gold'] = inventory['gold'] +50


#9

See this:

inventory = {
    'gold' : 500,
    'pouch' : ['flint', 'twine', 'gemstone'], # Assigned a new list to 'pouch' key
    'backpack' : ['xylophone','dagger', 'bedroll','bread loaf']
}

# Adding a key 'burlap bag' and assigning a list to it
inventory['burlap bag'] = ['apple', 'small ruby', 'three-toed sloth']

# Sorting the list found under the key 'pouch'
inventory['pouch'].sort() 

# Your code here
inventory['pocket'] = ['seashell', 'strange berry', 'lint']
inventory['backpack'].sort()
inventory['backpack'].remove('dagger')
inventory['gold'] = inventory['gold'] + 50
inventory['gold'] = inventory['gold'] + 50