计算还清贷款需要多长时间

2024-04-16 08:24:06 发布

您现在位置:Python中文网/ 问答频道 /正文

我一直在练习还清贷款需要多长时间。我试图做一个循环,计算每个月付款后的利息和新余额。到目前为止,我有以下几点:

def conta ():
  total = float(input('How much do you want to borrow?'))
  qtoPg = float(input('What is the monthly payment amount?'))
  annualInterest = float(input('What is the annual interest rate expressed as a percent?'))
  rateMonth = (annualInterest*0.01)/12
  tjm = total*rateMonth
  nbalance = total+tjm-qtoPg
  while nbalance <= qtoPg:
    tjm = nbalance*rateMonth
  print (nbalance)

Tags: theinputisfloatwhat余额total多长时间
2条回答

我不明白这是怎么回事

您有一个循环,其中退出条件比较nbalanceqtoPg,但在循环内部,这些值不会更改

而且,你的基础数学是错误的。每个月,余额按付款金额递减,但按到期利息递增,到期利息是根据余额而不是原始本金计算的

这应该使用EMI概念。将每月利息加在总金额上,减去每月支付的EMI金额。当余额小于或等于零时,则中断循环

下面修改的功能将打印要支付的EMI数量

def conta ():
  
  total = float(input('How much do you want to borrow? '))
  qtoPg = float(input('What is the monthly payment amount? '))
  annualInterest = float(input('What is the annual interest rate expressed as a percent? '))
  
  rateMonth = (annualInterest*0.01)/12
  
  nbalance = total
  emis = 0
  
  while nbalance > 0:
    emis += 1
    nbalance = nbalance + (rateMonth*nbalance)
    
    nbal = qtoPg
    if nbalance < qtoPg:
      nbal = nbalance
    
    nbalance -= qtoPg

    if nbalance <= 0:
      print(nbal)
    
  print (emis)

相关问题 更多 >