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


why the for loop doesn't work?

# Your code here
inventory['pocket'] = ['seashell', 'strange berry', 'lint']
for ['gold'] in inventory:
    ['gold'] += 50


why would you use a for loop?

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


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


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?


next is add this code perhaps?

for gold in inventory:
    print gold += 50

but I got error below:

    print gold += 50
SyntaxError: invalid syntax


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:

burlap bag

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']

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


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!


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"]


Could you help me too?


please make a new topic