Good day,
The purpose of my script is to create a small dictionary that is saved to a file using pickle and print the entire contents of the dictionary for the user. My script prompts the user to make 1 of 3 choices:
Selection 1 is intended to print all contents of a dictionary loaded using pickle. Selection 2 appends to the existing dictionary and prints what was appended. Selection 3 creates a file and adds an entry into it.
When I select option 1, only the first entry prints.
When I use select option 2, I can add an entry and I can see that entry in the file that is in the same folder as the script. I am unsure what type of file it is. I can open with notepad though. The new entry also prints.
I only select option 3 once, to create the file. It adds the first entry and prints it successfully.
I need help getting the entire contents of the dictionary to print.
print("""\n
1- View codes
2- Add code
3- Create file/add first""")
while True:
choice = input("What is your choice?")
DC_Message = {}
if choice == "1":
filename = "dcoutput"
infile = open(filename, 'rb')
DC_Message2 = pickle.load(infile)
infile.close()
for key, value in DC_Message2.items():
print(key, ' : ', value)
elif choice == "2":
DCcode = str(input("What is the new DCcode?"))
Message = str(input("What is the new message"))
DC_Message[DCcode] = Message
print(DC_Message)
for key, value in DC_Message.items():
outfile = open("dcoutput", 'ab')
pickle.dump(DC_Message, outfile)
outfile.close()
print(key, ' : ', value)
elif choice == "3":
DCcode = str(input("What is the new DCcode?"))
Message = str(input("What is the new message"))
DC_Message[DCcode] = Message
print(DC_Message)
for key, value in DC_Message.items():
outfile = open("dcoutput", 'wb')
pickle.dump(DC_Message, outfile)
outfile.close()
print(key, ' : ', value)