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


#1



why the for loop doesn't work?


# Your code here
inventory['pocket'] = ['seashell', 'strange berry', 'lint']
inventory['backpack'].sort()
inventory['backpack'].remove('dagger')
for ['gold'] in inventory:
    ['gold'] += 50


#2

why would you use a for loop?

and even if you use a for loop, why ['gold'] as iterator?


#3

just wondering if for loops could work too.. :smile: I have tried simple gold as iterator still it doesn't work.


#4

that sounds like a much better choice for an iterator:

for gold in inventory:
    print gold

so now you have all the keys from your inventory dictionary, what is next?


#5

next is add this code perhaps?

for gold in inventory:
    print gold += 50

but I got error below:

    print gold += 50
                ^
SyntaxError: invalid syntax

#6

you can't print and increase on the same line, i added a print for you to see what is in the iterator. As you can see, it contains all keys:

for gold in inventory:
    print gold

would output:

pocket
backpack
pouch
burlap bag
gold

so now you have all the keys. so to get the value:

for gold in inventory:
    print inventory[gold]

which would output:

['seashell', 'strange berry', 'lint']
['bedroll', 'bread loaf', 'xylophone']
['flint', 'gemstone', 'twine']
['apple', 'small ruby', 'three-toed sloth']
500

you only need to increase one value, so i don't see the point of a loop

using the loop, you would need to make changes to all values


#7

ah i see, so it's best not to use for loops in dictionaries because to change one value we need to change all the values.The gold is just iterator not the key. Thank you!


#8

well, you can use a loop if you want to change all the values, but that is not desired here

yes, exactly :slight_smile:

you should just use inventory["gold"]


#9

Uploading...
Could you help me too?


#10

please make a new topic


#11