首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >Python金融知识词典

Python金融知识词典
EN

Stack Overflow用户
提问于 2019-04-09 16:51:17
回答 1查看 204关注 0票数 1

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

代码语言:javascript
复制
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()
EN

回答 1

Stack Overflow用户

发布于 2019-04-10 19:08:29

在编写本文时,您的函数都有如下签名:

def savingsPlacement(person):

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

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

如果这不是一个任务,你会发现通过这种方式得到的程序更有用,更通用。

票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/55588704

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档