python - 根据之前的值循环一个函数?

标签 python

所以我尝试循环我的公式:pv = pv - p - (r * pv),60 次来计算 5 年后(60 个月)后的当前债务,以做到这一点对于每个新的计算,我需要之前的值 pv 来计算新值。

这是我到目前为止得到的,但我没有得到正确的最终值......

def functionname(pv, rent, year):
    r = (rent / 100) / 12
    p = pv * (r * ((1 + r) ** (year*12))) / (((1 + r) * (year*12)) - 1)
    for i in range(0, 61):
        pv = pv - p - (r * pv)
    return pv


pv = int(input("Type your debt: "))
rent = float(input("Type yearly rent: "))
year = int(input("Type how many years you want to pay of the loan: "))

pv = functionname(pv, rent, year)
print("The current debt after 5 years is:",pv)

最佳答案

如果要循环 N 次,只需将单个参数 N 传递给 range 函数即可。例如这个循环将运行 60 次。您当前的 for 循环运行了 61 次(从 0 到 60)。

def functionname(pv, retn, year):
    r = (rent / 100) / 12
    p = pv * (r * ((1 + r) ** (year*12))) / (((1 + r) * (year*12)) - 1)
    for i in range(60):
        pv = pv - p - (r * pv)
    return pv


pv = int(input("Type your debt: "))
rent = float(input("Type yearly rent: "))
year = int(input("Type how many years you want to pay of the loan: "))

pv = functionname(pv, rent, year)
print("The current debt after 5 years is:",pv)

关于python - 根据之前的值循环一个函数?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/54722242/

相关文章:

python - 使用与 read_csv 相同的逻辑将字符串转换为 Pandas 或 Numpy dtype?

python - 如何更改 Pytorch CNN 来拍摄彩色图像而不是黑白图像?

python - 由exec(variable =)在for循环中分配的变量范围?

python 两个大型二维列表之间的快速均方误差

python - 使用分层列连接两个 DataFrame

Python:高效调用多个返回函数的子集变量

python - Fabric : connect to machine with one user then do stuff with another

Python,如何根据列表重命名多个文件?

python - 如何将图例放在 seaborn.FacetGrid 的第一个子图中?

python - 值错误: setting an array element with a sequence in Python