JD2775
JD2775

Reputation: 3801

Refining a Python finance tracking program

I am trying to create a basic program that tracks retirement finances. What I have got so far below takes input for ONE entry and stores it. The next time I run it, the previous values get wiped out. Ideally what I would like is a program that appends indefinitely to a list, if I open it up 2 weeks from now I'd like to be able to see current data in dict format, plus add to it. I envision running the script, entering account names and balances then closing it, and doing it again at a later point

Few questions:

  1. How do I achieve that? I think I need some loop concept to get there
  2. Is there a more elegant way to enter the Account Name and Balance, rather than hardcoding it in the parameters like I have below? I tried input() but it only runs for the Account Name, not Balance (again, maybe loop related)
  3. I'd like to add some error checking, so if the user doesn't enter a valid account, say (HSA, 401k or Roth) they are prompted to re-enter. Where should that input/check occur?

Thanks!

from datetime import datetime

Account = {
    "name": [],
    "month": [],
    "day": [],
    "year": [],
    "balance": []
}

finance = [Account]

def finance_data(name, month, day, year, balance):

    Account['name'].append(name)
    Account['month'].append(month)
    Account['day'].append(day)
    Account['year'].append(year)
    Account['balance'].append(balance)
    print(finance)


finance_data('HSA',
        datetime.now().month,
        datetime.now().day,
        datetime.now().year, 
        500)

Upvotes: 0

Views: 79

Answers (1)

Rohan Varma
Rohan Varma

Reputation: 1195

When you run a script and put values in variables defined in the code, the values only last for however long the program runs. Each time you run the script, it will start over from the initial state defined in the code and, thus, not save state from the last time you ran the script.

What you need is persistent data that lasts beyond the runtime of the script. Normally, we accomplish this by create a database, using the script to write new data to the database, and then, when the script runs next, read the old values from the database to remember what happened in the past. However, since your use case is smaller, it probably doesn't need a full blown database system. Instead, I would recommend writing the data to a text file and then reading from the text file to get the old data. You could do that as follows:

# read about file io in python here: https://docs.python.org/2/tutorial/inputoutput.html#reading-and-writing-files
dataStore = open("dataFile.txt", "r+") # r+ is the read and write mode

def loadDataToAccount(dataStore):


    Account = {
        "name": [],
        "month": [],
        "day": [],
        "year": [],
        "balance": []
    }

    for line in dataStore.read().splitlines():
        (name, month, day, year, balance) = line.split("|")
        Account['name'].append(name)
        Account['month'].append(month)
        Account['day'].append(day)
        Account['year'].append(year)
        Account['balance'].append(balance)
    return Account

Account = loadDataToAccount(dataStore)

Here I am assuming that we organize the text file so that each line is an entry and the entry is "|" separated such as: bob|12|30|1994|500 rob|11|29|1993|499

Thus, we can parse the text into the Account dictionary. Now, lets look at entering the data into the text file:

def addData(Account, dataStore):
    name = raw_input("Enter the account name: ")
    balance = raw_input("Enter the account balance: ")
    # put name and balance validation here!

    month = datetime.now().month
    day = datetime.now().day
    year = datetime.now().year

    # add to Account
    Account['name'].append(name)
    Account['month'].append(month)
    Account['day'].append(day)
    Account['year'].append(year)
    Account['balance'].append(balance)

    # also add to our dataStore
    dataStore.write(name + "|" + month + "|" + day + "|" + year + "|" + balance + "\n")

addData(Account, dataStore)

Notice here how I wrote it to the dataStore with the expected format that I defined to read it in. Without writing it to the text file, it will not save the data and be available for the next time you run.

Also, I used input to get the name and balance so that it is more dynamic. After collecting the input, you can put an if statement to make sure it is a valid name and then use some sort of while loop structure to keep asking for the name until they enter a valid one.

You would probably want to extract the code that adds the values to Account and put that in a helper function since we use the same code twice.

Good luck!

Upvotes: 2

Related Questions