【问题标题】:Amortization Schedule on PythonPython 上的摊销时间表
【发布时间】:2021-10-09 21:44:02
【问题描述】:

我一直在使用 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)))

Output

【问题讨论】:

  • 在循环中的print 之后添加行startingBalance = endingBalance,年利率应为.03 而不是3

标签: python amortization


【解决方案1】:

您的代码一切正常,碰巧minterestCharge 都等于250 000,所以您的endingBalance 永远不会改变。问题可能出在你的数学上。

【讨论】:

    【解决方案2】:

    获取更新码需要

    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
    
    

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

    【讨论】:

    • 感谢您的帮助。我最近开始用 python 编码,也喜欢金融,所以我认为这将是两者的完美结合。
    猜你喜欢
    • 2021-04-03
    • 2010-09-17
    • 1970-01-01
    • 1970-01-01
    • 2011-06-18
    • 1970-01-01
    • 1970-01-01
    • 2018-02-13
    相关资源
    最近更新 更多