for 循环后在控制台中获取 inf 结果

问题描述 投票:0回答:1

在问题中使用测试用例后,我得到了 inf 结果

balance = 42
annualInterestRate = 0.2
monthlyPaymentRate = 0.04
def AfterBalance(balance,annualInterestRate,monthlyPaymentRate):
  monthlyInterestRate = annualInterestRate / 12.0
for i in range(12):
    monthlyPaymentRate = monthlyPaymentRate * balance
    monthlyUnpaid = balance - monthlyPaymentRate
    balance = monthlyUnpaid + monthlyInterestRate * monthlyUnpaid
  return round(balance,2)
LastBalance = AfterBalance(balance,annualInterestRate,monthlyPaymentRate)
print("Remaining balance", LastBalance)

我预计案件结果为 31.38 我只需要知道 inf 结果是如何形成的。 感谢您的帮助。

python for-loop
1个回答
0
投票
balance = 42
annualInterestRate = 0.2
monthlyPaymentRate = 0.04

def AfterBalance(balance, annualInterestRate, monthlyPaymentRate):
    monthlyInterestRate = annualInterestRate / 12.0
    for i in range(12):
        monthlyPayment = monthlyPaymentRate * balance
        monthlyUnpaid = balance - monthlyPayment
        balance = monthlyUnpaid + monthlyInterestRate * monthlyUnpaid
    return round(balance, 2)

LastBalance = AfterBalance(balance, annualInterestRate, monthlyPaymentRate)
print("Remaining balance", LastBalance)

输出:

Remaining balance 31.38
© www.soinside.com 2019 - 2024. All rights reserved.