14/14


#1

Hey everyone. Could anyone point me in the right direction? Here 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.append('pocket')
'pocket' : ['seashell', 'strange berry', 'lint']
inventory['backpack'].sort()

'backpack'.remove('dagger')
'gold' :500 + 50

#2

look at this code:

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

we add a key (burlap bag) to dictionary and the value is a list. Now look at your code:

inventory.append('pocket')
'pocket' : ['seashell', 'strange berry', 'lint']

looks slightly different. also, append() is a method for appending to a list, currently you are attempting it on a dictionary, that won’t work

here:

'backpack'.remove('dagger')

backpack is a key of inventory dictionary. backpack only lives inside the dictionary, so we need to access it as such

here:

'gold' :500 + 50

same for gold, gold only lives inside the dictionary

You made a bit of a mess, maybe start over and revisit some of the older exercise to better grasps things? Maybe find another source where you can read something about dictionaries?


#3

Ahh ok I see what I did wrong here. I changed my code a bit the only part I’m still confused about is the gold part.

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’]
backpack.sort()
inventory[‘backpack’].remove(‘dagger’)

inventory[‘gold’] : 550


#4

Nevermind thanks for the help figured it out!


#5

good you figured it out on your own :slight_smile: Do you understand it? Its syntax is the same as regular variable update/re-assignment, just now a key for a dictionary


#6

is there another way to modify Gold key, instead or just setting like: inventory[“gold”] = 550 ?


#7

of course there is, just like with updating a regular variable we need to store the current value in the variable:

x = x + 5

the same can be done with dictionary keys


#8

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