RoyalSwish
RoyalSwish

Reputation: 1565

Python: Saving a User's Session

I have made a program using Python which is essentially a dictionary of computing terms. The program gives the user the ability to add their own terms with their respective definitions and the program will save it but only for that session, making it useless for the user.

How do I make the program save the user's terms and definitions so they are not lost when the program is closed? I honestly have no idea how to go about this. This is only a CLI program and I haven't made it into an executable, if that helps you.

terms = {"ALU":"Arithmetic Logic Unit", "CPU":"Central Processing Unit", "GPU":"Graphics Processing Unit"}
def print_menu():
    print('Computing Terms')
    print()
    print('0. Quit')
    print('1. Look Up a Term')
    print('2. Add a Term')
    print('3. Redefine a Term')
    print('4. Delete a Term')
    print('5. Display All Terms')

while True:
    print_menu()
    print()
    choice = int(input('Choice: '))
    if choice == 0:
        break
    elif choice == 1:
        print('\n')
        term = input('Type in a term you wish to see: ')
        if term in terms:
            definition = terms[term]
            print('\n')
            print(term, '-', definition, '\n')
            print()
            print('----------------------------------------------------------------')
            print()
            print()
        else:
            print('This term does not exist. Try adding it instead.\n')
            print()
            print('----------------------------------------------------------------')
            print()
            print()
    elif choice == 2:
        print('\n')
        term = input('What term would you like to add?: ')
        if term not in terms:
            print('\n')
            definition = input('What\'s the definition?: ')
            terms[term] = definition
            print('\n')
            print(term, 'has been added.\n')
            print()
            print('----------------------------------------------------------------')
            print()
            print()
        else:
            print('\n')
            print('Term already exists, try redefining it instead.\n')
            print()
            print('----------------------------------------------------------------')
            print()
            print()
    elif choice == 3:
        print('\n')
        term = input('Which term do you want to redefine?: ')
        if term in terms:
            definition = input('What\'s the new definition?: ')
            terms[term] = definition
            print('\n')
            print(term, 'has been redefined.\n')
            print()
            print('----------------------------------------------------------------')
            print()
            print()
        else:
            print('\n')
            print('That term doesn\'t exist, try adding it instead.')
            print()
            print('----------------------------------------------------------------')
            print()
            print()
    elif choice == 4:
        print('\n')
        term = input('Which term would you like to delete?: ')
        if term in terms:
            del terms[term]
            print('\n')
            print('The term has been deleted.\n')
            print()
            print('----------------------------------------------------------------')
            print()
            print()
        else:
            print('\n')
            print('This term doesn\'t exist.')
            print()
            print('----------------------------------------------------------------')
            print()
            print()
    elif choice == 5:
        print('\n')
        print('The terms available are: ')
        for term in terms:
            print(term)
        print()
        print()
        print('----------------------------------------------------------------')
        print()
        print()
    else:
        print('\n')
        print('Sorry, but ', choice, ' is not a valid choice.\n')
        print()
        print('----------------------------------------------------------------')
        print()
        print()

Upvotes: 0

Views: 110

Answers (1)

Ketouem
Ketouem

Reputation: 3857

Have a look at the pickle module, which provides a way of (de)serializing data structures (in your case a dict) into a file that you can load/dump when needed.

https://wiki.python.org/moin/UsingPickle

Upvotes: 3

Related Questions