在给定的代码中,我正在进行财务计划。涉及金融素养(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()
答案 0 :(得分:0)
在撰写本文时,您的函数都具有如下签名:
def savingsPlacement(person):
但是,它们都不使用传入的参数person
。而是直接引用fl
和nfl
变量。
这可能是一项任务。如果是这样,则需要进行返工。我将从放置该版本的副本开始,然后重新开始。选择一个函数,例如savingsPlacement()
,然后仅编写该函数,而完全不引用变量名fl或nfl。而是仅使用变量person
。在使用该功能时,仅使用您所了解的人。您可能需要为某些函数中硬编码的其他值添加另一个参数。通过使用Python命令行中的参数调用函数来测试该函数。然后再继续执行其他功能。
如果这不是一项任务,那么无论如何您都会发现生成的程序更加有用和通用。