I am trying to teach myself Python. I have started out creating several small programs. This one area has me stumpped. I have created a simple password saver program. I thought using a dictionary would be the best option. The only problem I have ran into is I cannot get the dictionary to save after I close my program. I have searched google and this site and cannot find any piece of code to fix it.
I want to be able to increase the number of entries in the dictionary by either hitting 2 to add or 3 to edit. I can add and edit all day. I can recall them as long as I do not exit the program. How do I get the password dictionary to update and save before I exit?
Thanks
#John
#Password program
#The actual dictionary
password = {"aol" : "thepassword" ,
"AKO" : "akopassword" , }
choice = None
while choice != "0":
print \
"""
Password Saver Program
0 - Quit
1 - Look up a password
2 - Add a password
3 - Change a password
4 - Delete a password
"""
choice = raw_input("Choice: ")
print
#exit
if choice == "0":
print "Good-bye."
#get a password
elif choice == "1":
term = raw_input("What password do you want me to retreive?: ")
if term in password:
definition = password[term]
print "\n", term, "the password is" , definition
else:
print "\nSorry, I don't know", term
# add a term-definition pair
elif choice == "2":
term = raw_input("What password do you want me to add?: ")
if term not in password:
definition = raw_input("What's the password?: ")
password[term] = definition
print "\n", term, "has been added."
else:
print"\nThat term already exists! Please try again."
#change an existing password
elif choice == "3":
term = raw_input ("What password do you want me to redefine?: ")
if term in password:
definition = raw_input("What's the new definition?: ")
password[term] = definition
print "\n", term, "has been redefined."
else:
print "\nThat term doesn't exist! Try adding it."
#delete a password
elif choice == "4":
term = raw_input("What password do you want me to delete?: ")
if term in password:
del password[term]
print "\nOk, I deleted", term
else:
print"\nI can't do that!", term, "doesn't exist in dictionary."
#some unknown choice
else:
print"\nSorry, but", choice, "isn't a valid choice."
raw_input("\n\nPress the enter key to exit.")