【问题标题】:Python Dictionary on Financial LiteracyPython 金融知识词典
【发布时间】:2019-08-30 12:06:38
【问题描述】:

在给定的代码中,我正在做财务规划。这涉及金融知识 (fl) 的人,而不是金融知识 (nfl) 的人。我必须计算他们 40 年的储蓄、贷款支付、住房支付后的余额,并将其绘制在图表上。它涉及两个字典 fl 和 nfl,我不确定如何在函数中输入字典,因为最后一个函数“模拟”需要调用所有函数,并且如果我只在最后一个函数中输入它会显示错误

import numpy as numpy
import matplotlib.pyplot as plt
import matplotlib 

fl = {"savings": 5000, "checking": 1000, "debt": 30100, "loan": 0, "yearsWithDebt": 0, "yearsRented": 0, "debtPaid": 0}
nfl = {"savings": 5000, "checking": 1000, "debt": 30100, "loan": 0, "yearsWithDebt": 0, "yearsRented": 0, "debtPaid": 0}
#nfl["savings"]=nfl["savings"]*1.01
#print(range(12))
house_var = False
house_var_1 = False
def savingsPlacement(person):
    """ 
    This function simulates the increasing interest on a person's savings account depending on whether
    they put it in a bank account or a mutual fund. 
    input: a dictionary representing fl or nfl
    output: an updated dictionary with the new savings amount after 1 year of it being in
    either the mutual fund of the bank account
    """ 
    nfl["savings"]=nfl["savings"]*1.01
    fl["savings"]=fl["savings"]*1.07

    return person
def debt(person):
    """ 
    This function simulates the amount of debt a person has left and the amount they
    paid after one year.
    input: a dictionary representing fl or nfl
    output: an updated dictionary. debt, savings, debtPaid, and yearsWithDebt
    are all changed each year if there is debt remaining.
    """
    if(nfl["debt"]>0):
        j=0
        for i in range(12):
            nfl["debt"]=nfl["debt"]-(0.03*nfl["debt"]+1)
            nfl["debtPaid"]=0.03*nfl["debt"]+1
        nfl["debt"]=1.2*nfl["debt"]
        j=j+1;
        nfl["yearsWithDebt"]=j

    if (fl["debt"]>0):
        k=0
        for i in range(12):
            fl["debt"]=fl["debt"]-(0.03*fl["debt"]+15)
            fl["debtPaid"]=0.03*fl["debt"]+15
        fl["debt"]=1.2*fl["debt"]
        k=k+1;
        fl["yearsWithDebt"]=k

    return person

def rent(person):
    """ 
    This function simulates the amount of money a person has left in their bank account
    after paying a year's worth of rent.
    input: a dictionary representing fl or nfl
    output: an updated dictionary with a checking account that has been lowered by the
    amount the person had to pay for rent that year.
    """
    nfl["checking"]=nfl["checking"]-850
    fl["checking"]=fl["checking"]-850

def house(person):
    """
    This function simulates the amount of money a person has left in their bank accont
    after paying monthly mortgage payments for a year.
    input: a dictionary representing fl or nfl
    output: an updated dictionary with a loan and checking account lowered by the
    mortgage payments made that year.
    """
    if house_var==True :
        for j in range(12):
            N = 360
            D = ((0.05 + 1) * N - 1) / (0.05 * (1 + 0.05) * N)
            P = 175000 / D
            nfl["checking"] = nfl["checking"] - P
            nfl["loan"] = (175000-0.05*175000) - P
    if house_var_1==True:
        for j in range(12):
            N = 360
            D = ((0.045 + 1) * N - 1) / (0.045 * (1 + 0.045) * N)
            P = 175000 / D
            fl["checking"] = fl["checking"] - P
            fl["loan"] = (175000-0.2*175000) - P

    return  person

def simulator(person):
    """ 
    This function simulates financial decisions over the course of 40 years.
    input: a dictionary representing fl or nfl
    output: a list of intergers representing the total sum of money that fl
    or nfl has each year. 
    """
    simulator()
    """for i in range(40):
        fl["wealth"]=fl["savings"]+fl["checking"]-fl["debt"]-fl["loan"]
        nfl["wealth"]=nfl["savings"]+nfl["checking"]-nfl["debt"]-nfl["loan"]
        fl["checking"]=fl["checking"]+0.3*29500
        fl["savings"]=fl["savings"]+0.2*29500
        nfl["checking"]=nfl["checking"]+0.3*29500
        nfl["savings"]=nfl["savings"]+0.2*29500
        savingsPlacement(person)
        debt(person)
        if(nfl["checking"]>0.05*175000):
            house_var=True
            house(person)
        if(fl["checking"]>0.2*175000):
            house_var_1=True
    return fl["wealth"],nfl["wealth"],person

"""
datafl = simulator(fl)
datanfl = simulator(nfl)

plt.xlabel('Years')
plt.ylabel('Wealth')
plt.title('Wealth of fl vs nfl Over 40 Years')
plt.plot(datafl, label='fl')
plt.plot(datanfl, label='nfl')
plt.legend()
plt.show()

【问题讨论】:

  • 错误是什么?请显示异常回溯。

标签: python python-3.x python-2.7 jupyter-notebook


【解决方案1】:

在撰写本文时,您的函数都有这样的签名:

def savingsPlacement(person):

但是,它们都没有使用传入的参数person。相反,它们直接引用了flnfl 变量。

这可能是一项任务。如果是这样,则需要返工。我会先把这个版本的副本放在一边,然后重新开始。选择一个函数,例如savingsPlacement(),只写那个函数,根本不引用变量名fl 或nfl。相反,只使用变量person。仅使用您在进入该功能时可以了解的有关人员的信息。您可能需要为某些函数中硬编码的其他值添加另一个参数。通过使用 Python 命令行中的参数调用该函数来测试该函数。然后才继续执行另一个功能。

如果这不是一个作业,那么无论如何,您会发现生成的程序更有用且更通用。

【讨论】:

  • 是的,我意识到了这一点,并根据函数中的参数更改了代码,后来又编辑了几行代码,使代码运行。谢谢你的帮助。
猜你喜欢
  • 2011-01-16
  • 2021-11-17
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2018-04-19
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多