【问题标题】:Refining a Python finance tracking program完善一个 Python 财务跟踪程序
【发布时间】:2018-06-24 17:43:56
【问题描述】:

我正在尝试创建一个跟踪退休财务的基本程序。到目前为止,我在下面得到的内容是 ONE 条目的输入并存储它。下次我运行它时,以前的值会被清除。理想情况下,我想要一个无限期附加到列表的程序,如果我从现在开始 2 周打开它,我希望能够以 dict 格式查看当前数据,并添加到它。我设想运行脚本,输入账户名称和余额,然后关闭它,稍后再做一次

几个问题:

  1. 如何实现?我想我需要一些循环概念才能到达那里
  2. 有没有一种更优雅的方式来输入账户名称和余额,而不是像下面这样在参数中硬编码?我尝试了 input() 但它只针对帐户名称运行,而不是余额(再次,可能与循环相关)
  3. 我想添加一些错误检查,因此如果用户没有输入有效帐户,例如(HSA、401k 或 Roth),系统会提示他们重新输入。该输入/检查应该在哪里进行?

谢谢!

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)

【问题讨论】:

  • 如果要在程序关闭后存储,需要使用文件或数据库来存储值。

标签: python function loops dictionary


【解决方案1】:

当您运行脚本并将值放入代码中定义的变量中时,这些值只会持续到程序运行的时间。每次运行脚本时,它都会从代码中定义的初始状态重新开始,因此不会保存上次运行脚本时的状态。

您需要的是超出脚本运行时间的持久性数据。通常,我们通过创建一个数据库来完成此操作,使用脚本将新数据写入数据库,然后,当脚本下次运行时,从数据库中读取旧值以记住过去发生的事情。但是,由于您的用例较小,它可能不需要完整的数据库系统。相反,我建议将数据写入文本文件,然后从文本文件中读取以获取旧数据。你可以这样做:

# 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)

这里我假设我们组织文本文件,使得每一行都是一个条目,条目是“|”分开如: 鲍勃|12|30|1994|500 抢|11|29|1993|499

因此,我们可以将文本解析到 Account 字典中。现在,让我们看看将数据输入到文本文件中:

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)

请注意我是如何使用我定义的预期格式将其写入 dataStore 以读取它的。如果不将其写入文本文件,它将不会保存数据并在您下次运行时可用。

此外,我使用输入来获取名称和平衡,使其更具动态性。收集输入后,您可以放置​​一个 if 语句以确保它是一个有效的名称,然后使用某种 while 循环结构不断询问该名称,直到他们输入一个有效的名称。

您可能希望提取将值添加到 Account 的代码并将其放入辅助函数中,因为我们两次使用相同的代码。

祝你好运!

【讨论】:

  • 请记住通过data.close()关闭文件缓冲区或使用with语句(上下文管理器)打开文件。
  • @Rohan Varma 谢谢!问题,当我运行您的代码时,它会提示我输入帐户名称,但程序会在该点停止并且不会提示输入帐户余额,也不会向文本文件写入任何内容。它对你有用吗?感谢您花时间写下所有内容并进行解释,非常有帮助!
  • @JD2775 啊,我正在做的输入有一个问题。您想使用“raw_input”而不是“input”,因为“raw_input”只会获取用户输入的字符串。我通过上面的答案进行了编辑以反映这一点。另外,请确保您使用 python 代码在目录中创建一个空的“dataFile.txt”,因为以“r+”模式打开它假定它已经存在,因此如果您还没有创建该文件,它将崩溃。乐意效劳!如果您在此之后仍有任何问题,请告诉我。
  • @RohanVarma raw_input 抛出错误。我认为它是 Python 3 的东西,我认为我需要使用 input 而不是 raw_input。没什么大不了的,你让我有了一个好的开始。我可以玩弄它,看看我能不能弄明白!再次感谢
  • @RohanVarma 原来是 Sublime Text 的问题。当我在 Pycharm 中运行程序时,它运行良好(使用输入)并循环通过两个输入,并写入文本文件。再次感谢
猜你喜欢
  • 2010-10-22
  • 2017-09-05
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2020-02-02
  • 1970-01-01
相关资源
最近更新 更多