1

我一直在使用 python 制定摊销计划。但是,当我运行代码时,输​​出并没有给我正确的结果,相反,它似乎循环并打印了 360 次相同的值。下面是我写的代码。

import pandas as pd
import math

p = float(input("Amount borrowed: "))
r = float(input("Annual Interest Rate: "))
t = float(input("Payback Period in years: "))

m=(p*(r/12)*(math.pow(1+r/12,12*t)))/(math.pow(1+r/12,12*t)-1)
print()
print("Your payment will be: $"+str(m))
print()
print("Month\tStartingBalance\tInterestCharge\tPayment\tEndingBalance")
month=12*t
month=int(month)
startingBalance=p
for i in range(1,month+1):
  interestCharge=r/12*startingBalance
  endingBalance=startingBalance+interestCharge-m
  print(str(i)+"\t$"+str(round(startingBalance,2))+"\t$"+str(round(interestCharge,2))+"\t$"+str(round(m,2))+"\t$"+str(round(endingBalance,2)))

输出

4

2 回答 2

1

您的代码一切正常,只是碰巧两者minterestCharge都等于 250 000,因此您endingBalance永远不会改变。问题可能与您的数学有关。

于 2021-10-09T21:52:34.297 回答
0

获取更新代码需要

startingBalance = endingBalance

根据 https://www.investopedia.com/terms/a/amortization_schedule.asp输入数据的修改示例

import pandas as pd
import math

#p = float(input("Amount borrowed: "))
#r = float(input("Annual Interest Rate: "))
#t = float(input("Payback Period in years: "))

# sample data entered instead
p = float(250e3)
r = float(4.5*.01)
t = float(30)


m=(p*(r/12)*(math.pow(1+r/12,12*t)))/(math.pow(1+r/12,12*t)-1)
print()
print("Your payment will be: $"+str(m))
print()
print("Month\tStartingBalance\tInterestCharge\tPayment\t\tEndingBalance")
month=12*t
month=int(month)
startingBalance=p
for i in range(1,month+1):
  interestCharge = r/12*startingBalance
  endingBalance = startingBalance+interestCharge-m
  argI  = str(i)
  argSB = str(round(startingBalance,2))
  argIC = str(round(interestCharge,2))
  argM  = str(round(m,2))
  argEB = str(round(endingBalance,2))
  msg = argI + "\t$" + argSB + "\t\t$" + argIC + "\t\t$" + argM + "\t\t$"+argEB
  print(msg)
  startingBalance = endingBalance

我希望它对你有帮助。玩得开心

于 2021-10-09T22:14:49.067 回答