14. It's Dangerous to Go Alone! Take This


#1

I am baffled here. This is my code:

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']+=50

I recieve this error: Traceback (most recent call last):
File "python", line 17, in
KeyError: 'backpack'

And this error: Oops, try again. Check for keys without values or misspelled keys!

I keep trying different things, and have searched the community, copy/pasted, still can't get this through.


#2

look at this code:

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']
}
print inventory

its mostly your code, with one additional print statement.

the print statement shows you didn't add pocket to inventory, you made pocket the only key in inventory.


#3

Duh, should have known about that, to used to Javascript. However I am still getting the error, seemes to be something with this: inventory['backpack'].sort()
Again the error: Traceback (most recent call last):
File "python", line 17, in
KeyError: 'backpack'

And: Oops, try again. Check for keys without values or misspelled keys!

Please be patient with me.:slight_smile::stuck_out_tongue:


#4

i see an updated version of your code?


#5

Yeah, no problem.

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']+=50
print inventory

I know it ought to be something obvious.


#6

here:

inventory = {
    'pocket' : ['seashell', 'strange berry', 'lint']
}

you redifine inventory, making pocket the only item in inventory dictionary

Instead, you should have added pocket to inventory


#7

Oh, ok, I got it now. Should have seen that.:expressionless: Thank you.


#8

You have indeed seem to missed it when i provided you earlier with code where i added a print statement showing your inventory was pretty empty

All resolved now?


#9

Yes it is thanks. Didn't quite understand what you were going for at first.


#10