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.")

Recommended Answers

All 2 Replies

The proper way to save a dictionary object is with Python module pickle. Here is an example:

# use module pickle to save/dump and load a dictionary object
# or just about any other intact object
# use modes "wb" and "rb" to work with Python2 and Python3

import pickle

# create the test dictionary
before_d = {}
before_d[1]="Name 1"
before_d[2]="Name 2"
before_d[3]="Name 3"

# pickle dump the dictionary to a file
fout = open("dict1.dat", "wb")
pickle.dump(before_d, fout)
fout.close()

# pickle load the dictionary from a file
fin = open("dict1.dat", "rb")
after_d = pickle.load(fin)
fin.close()

print(before_d)  # {1: 'Name 1', 2: 'Name 2', 3: 'Name 3'}
print(after_d)   # {1: 'Name 1', 2: 'Name 2', 3: 'Name 3'}
commented: nice +4

Thanks. Working the code now. Running into a few issues. This is my first programming language I have tried to learn.

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.