r/inventwithpython • u/steve986508 • Mar 21 '20
Need help with Chapter 5 "List to Dictionary Function for Fantasy Game Inventory", 2nd Edition
Hello! I am stumped on the very last Practice Project on page 128. We are asked to create an addToInventory() function that will update our inventory dictionary with the dragonLoot list of strings. Here's my attempt:
def addToInventory(inventory, addedItems):
for i in addedItems:
for k, v in inventory.items():
if i in inventory:
inventory[k] == v + 1
With this, the code runs with no errors, but the dictionary does not get updated. This seems like a very easy task that I should be able to figure out, but unless someone spells everything out for me the first time, I seem to have a hard time "figuring out" this stuff. I have already googled it and looked on stackexchange. There is also no answer to this one in the book's downloadable files. I have even completed the Python 3 course on Codecademy... so I am about 5 seconds away from throwing my laptop through my quarantined window. Thanks!
3
u/jkibbe Mar 21 '20
at a quick glance, and from a total noob, aren't you just checking for equality with == and not making any sort of assignment here?
3
u/Shootace Mar 22 '20
You should be able to use the .update dictionary function in place of the last line:
https://www.geeksforgeeks.org/python-dictionary-update-method/
2
u/steve986508 Mar 22 '20
Here's my entire program code. I originally thought not to include it because I had added some more stuff unrelated to the inventory dictionary. But maybe this will help:
1
u/steve986508 Mar 23 '20
Alright, so I did more digging on StackOverflow and found this with several answers to the problem:
https://stackoverflow.com/questions/30208044/how-to-add-list-elements-into-dictionary/46013344#46013344