It's dangerous to go alone!


#1



Hello there, so I was doing this exercise when I got the following error in line 16:

Traceback (most recent call last):
File "python", line 16, in
KeyError: 'backpack'"


This is the whole code. Any ideas?

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

Thanks in advance!!


#2

here:

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

you overwrote your existing inventory, now it only contains a pocket.No more backpack, so then when you try to sort backpack, it no longer exists, you get an error

try adding pocket to inventory without overwriting what is already inside


#3

Oh I see, thanks!
I'll do that now.


#4

This topic was automatically closed 7 days after the last reply. New replies are no longer allowed.