I am currently trying to make a inventory system, where the user can update the dictionary as they see fit. I thought I would use pickle since it seemed to save the dictionary for me. The only issue is now every time I reference the list, and then go back to add another item to the list it still erases what is there.
import pickle
#creates a dictonary
Inventory = {}
product_add = ""
Key = "Key \n+ changes stock, \ni or I shows inventory"
print(Key)
def main():
choice = input("What do you want to do today? ")
if choice == "+":
Change()
else:
inv()
def Change():
product = input("What product do you want to add/change? ")
value = input("How much of it? ")
Inventory.update({product: value})#places into dictonary
pickle.dump(Inventory, open("save.p", "wb"))
continu = input("Would you like to enter any more products into inventory? y/n ")
if continu == "y" or continu == "Y":#allows them to either continue to update inventory or move on
Change()
else:
inv()
def inv():#prints inventory
print()
print()
print()
Inventory = pickle.load(open("save.p", "rb"))
for key,val in sorted(Inventory.items()):#prints the dictonary in a column alphabetized
print(key, "=>", val)
print()
print()
print()
print(Key)
Pass = input("What would you like to do? ")
if Pass == "+":#keeps the program going, and allows them to change the dictonary later
Change()
else:
main()
main()
-
Could you describe exactly what behavior you are expecting and what different behavior you are observing? (I played a bit around with it and it worked fine for what I tested)Fabian N.– Fabian N.2017年04月18日 21:13:28 +00:00Commented Apr 18, 2017 at 21:13
-
1Make sure you're adding unique keys to the dictionary. Items in Python dictionaries are referred to by their key. An item is basically a key-value pair. Keys are always unique in dictionaries. Python won't complain if you try to add a key that's already there, it will just override it. The limitation of your program is that it can't (yet) cope with sets of values (i.e. multiple instances of a certain product).lennyklb– lennyklb2017年04月18日 21:20:57 +00:00Commented Apr 18, 2017 at 21:20
-
Also, you'll want to write function and variable names lowercase in Python.lennyklb– lennyklb2017年04月18日 21:28:22 +00:00Commented Apr 18, 2017 at 21:28
-
I'm wanting an empty dictionary so that when the user wants to add something to the dictionary it will add it to the dictionary without any issues. Although once I run the program with the pickle it resets the dictionary, but when I don't use the pickle and close the dictionary it resets it. I am trying to find a way to save what the user puts into the dictionary, even after they close and reopen the program. If this makes any sense.Linkstus– Linkstus2017年04月18日 21:40:02 +00:00Commented Apr 18, 2017 at 21:40
1 Answer 1
Each time your program starts, you create an empty Inventory with
Inventory = {}
You want to load the inventory you previously saved. Only in case no one already exists, you can create a new, empty one.
You could do it like that:
try:
Inventory = pickle.load(open("save.p", "rb"))
except FileNotFoundError:
Inventory = {}
There are some other issues in your code.
One is that your functions are recursively calling each other, which would lead your program to crash after some 1000 calls - and which makes the logic overcomplicated.
You should rather have a loop in main() that manages the menu, and calls your inv or change function, who would simply return when they're done.