It's Dangerous to Go it Alone 14/14 Error


#1

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 cannot seem to understand what the error is: It says
Traceback (most recent call last):
File "python", line 15, in
KeyError: 'backpack'
Check for keys without values or misspelled keys!


#2

The above line is wiping out the earlier defined members of the dictionary.

inventory['pocket'] = ...

will add a pocket member.


#3

Ok... I still don't get it. I've edited the code but seem to still be having mistakes.

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

Error messages: Traceback (most recent call last):
File "python", line 14, in
TypeError: unhashable type


#4

inventory['pocket'] = [ 'seashell', 'strange berry', 'lint']

#5

mtf, thank you for that help. It's definitely appreciated. Now though I'm getting another error asking if I deleted my inventory directory. I already reset the code. What's up??
Error message:
Oops, try again.
Did you accidentally delete the inventory dictionary? Click Reset Code to get back to the original 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() 

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

#6

In a statement, we make an assignment, as shown in my earlier post. Use = not :.

This will not add 50, but subtract 450. It should read,

inventory['gold'] += 50

Now it will equal 550.


#7

why is it += instead of just +?


#8

So that we augment the current amount. It's the same as writing,

g = 500
g = g + 50
print g    # 550

g -= 50
print g    # 500

The above is in the group of operation assignment operators.

Eg.

    a = 5         # value assigment

    b = a         # value copy assignment

    c = a + b     # value expression assignment

    u = {}        # reference object assignment

    v = u         # direct reference to `u`, not a copy

    s = []        # reference object assignment

    t = s         # direct reference to `s`, not a copy

    w = [''] * 5  # list reference object with 5 elements  .1

.1 Note: We cannot do this with dictionaries, only lists.

The above are most of the assignments we will typically define. We cannot augment/decrease an undefined variable, meaning we have to initialize the value of any variable we intend to mutate.

These operations augment the value:

    a *= 5         # value `a` is multiplied by `5`
    b += 5         # value `b` is increased by `5`
    a **= 2        # value `a` is raised to the power of `2`

These operations decrease the value:

    a /= 5         # value 'a' is divided by `5`
    a %= 5         # value `a` becomes the modulo when modulus is 5
    a //= 5        # value `a` is floor of `a` divided by `5`.
    b -= 5         # value `b` is reduced by `5`

Since we can assign multiple elements to a list, it follows that we should be able to augment it.

    w *= 2
    w += ['']  # .2

*.2 Note: We must populate the cells when we create and augment the list.


#9

thank you

p.s. this has to be at least 20 characters to post. i did it!


#10

Does the above make sense to you?


#12