首页
学习
活动
专区
工具
TVP
发布
社区首页 >问答首页 >Python是如何计算这个公式的?

Python是如何计算这个公式的?
EN

Stack Overflow用户
提问于 2018-09-20 05:20:14
回答 2查看 138关注 0票数 0

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

代码语言:javascript
复制
    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))

我只是试着通过范围(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使用这个公式所采取的步骤?

EN

回答 2

Stack Overflow用户

发布于 2018-09-20 05:38:36

我可以想象它是这样做的:

代码语言:javascript
复制
balance = 484 - (484 * 0.04) + ((484- (484 *0.04)) * (0.2/12))

这基本上就是它在那里写的内容,结果是472.38。

然后用472.38替换变量balance e,重复计算12次,总是用新的结果替换变量,最后返回361.61

这些计算对我来说,无论是在软件中还是在手工中都是有效的。

票数 1
EN

Stack Overflow用户

发布于 2018-09-20 07:19:08

为了清楚起见:

代码语言:javascript
复制
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))

提供:

代码语言:javascript
复制
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

拆分计算允许这样的事情:

代码语言:javascript
复制
>>> 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
票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/52414353

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档