Python如何计算此公式?

时间:2018-09-19 21:20:14

标签: python python-3.x

我正在解决这个问题,我知道这是正确的,但是我不确定该公式如何工作。

    balance: int = 484
    monthlyPayRate: float = 0.04
    annualInterestRate: float = .2
    for i in range(12):
        balance = balance - (balance * monthlyPaymentRate) +\
        ((balance - (balance * monthlyPaymentRate)) * \
        (annualInterestRate/12))
    print("Remaining balance:", round(balance,2))

我只是尝试在range(1)中工作,我知道正确的答案是472.38。

这是我的计算方式: 484 –(484 * 0.04)= $ 464.64(这是付款后但未计利息的余额) 464 *(.2 / 12)= $ 7.42(我们将余额464.64 x利率0.016取走) 464 .64 + 7.424 = $ 472(我们在剩余余额中加上了利息,以得到新的余额)

当我尝试将数字插入python公式并手动执行时,我无法弄清楚Python是如何使其工作的。我希望有人能向我展示Python使用公式采取的步骤?

2 个答案:

答案 0 :(得分:1)

我想它会像这样:

balance = 484 - (484 * 0.04) + ((484- (484 *0.04)) * (0.2/12))

基本上就是那里写的,结果是472.38。

但是随后它将472.38替换为变量balance,e再次进行了12次计算,始终用新结果替换变量,最后返回361.61

计算对于我来说都可以在软件中完成,也可以手动完成。

答案 1 :(得分:0)

为清楚起见:

balance = 484
monthlyPaymentRate = 0.04
annualInterestRate = .2
for i in range(12):
    paidoff = balance * monthlyPaymentRate
    newinterest = (balance - paidoff) * annualInterestRate/12
    balance = balance - paidoff + newinterest
    print("Balance after", i+1, "months", round(balance,2));

print("Remaining balance:", round(balance,2))

给予:

Balance after 1 months 472.38
Balance after 2 months 461.05
Balance after 3 months 449.98
Balance after 4 months 439.18
Balance after 5 months 428.64
Balance after 6 months 418.35
Balance after 7 months 408.31
Balance after 8 months 398.51
Balance after 9 months 388.95
Balance after 10 months 379.62
Balance after 11 months 370.5
Balance after 12 months 361.61

Remaining balance: 361.61

拆分计算可以进行以下操作:

>>> balance = 484
>>> totalpaid=0
>>> totalinterest=0
>>> monthlyPaymentRate = 0.04
>>> annualInterestRate = .2
>>> for i in range(12):
...     paidoff = balance * monthlyPaymentRate
...     newinterest = (balance - paidoff) * annualInterestRate/12
...     balance = balance - paidoff + newinterest
...     totalpaid = totalpaid + paidoff
...     totalinterest = totalinterest + newinterest
...
>>> print("Remaining balance:", round(balance,2))
Remaining balance: 361.61
>>> print("Total amount paid off:", round(totalpaid,2))
Total amount paid off: 203.98
>>> print("Total interest accrued:", round(totalinterest,2))
Total interest accrued: 81.59