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


#1



https://www.codecademy.com/courses/python-beginner-en-pwmb1/2/5?curriculum_id=4f89dab3d788890003000096#

My error message was:
Traceback (most recent call last):
File "python", line 15, in
KeyError: 'backpack'

I am not sure what to do because the instructions said to .sort() the items in the list stored under 'backpack' and I can't think of any other way to do it

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


#2

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

I think your last line is only incorrect


#3

Thank you for helping me!


#4

No problem , always welcome :wink:


#5

Im having the same error, unfortunately.

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

code is:

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


#6

nevermind i figured it out. I was replacing the dict, with pocket on line 14.


#7

correct


#8

not working above code..


#9

Thanks, I didn't understand the question properly


#10

i tried this code but it still throws an error in the first backpack statement
'


#11

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

always make sure to include, inventory, with your list(because list is inside dictionary and inventory is the dictionary).

the rest of "how to do" is in the previous few chapters


#12


#13

A post was split to a new topic: Using sort()