请我尝试找出我的推理有什么问题,从而找出我的结果。
我正在学习一门在线课程,我需要计算 12 个月内消除信用卡债务所需的最低金额。我得到了年利率、债务金额(余额)的值以及每月还款额应增加的值(10 的倍数)。
根据我的推理,我生成的代码应该在几个月内迭代,但如果余额值不为零,则应该增加每月付款并重新计算。
我的价值观(我认为)与预期结果略有偏差。我的代码是这样的:
annualInterestRate = 0.2
monthlyInterestRate = annualInterestRate / 12.0
monthlyPayment = 10
while (balance > 0):
for each in range(0, 12):
balance = balance - monthlyPayment
balance = balance + (monthlyInterestRate * balance)
if (balance > 0):
monthlyPayment += 10
else:
break
print monthlyPayment
余额=3329,年利率0.2,
我的结果是:310(正确)
对于余额 = 4773,年利率为 0.2,
我的结果是:380(不正确,应该是440)
余额=3926,年利率0.2,
我的结果是:340(不正确,应该是360)。
请有人帮助启发我哪里错了?
Thanks!
你快到了。您的实施中存在一些问题。
首先,在意识到之前测试的每月付款没有支付后,您需要重置余额。
其次,检查余额并增加余额的选项卡是错误的。按照目前的情况,你每个月要多付 10 美元,如果我正确理解你的问题的话,这不是你想要的。您想增加每月还款额after看到少了 10 美元并没有在 12 个月内还清这笔钱。
正如另一点,你的else: break
是不必要的,因为它会打破while
当进入下一次迭代时循环。
startBalance = int(input("what's the stating balance? "))
balance = startBalance
numMonths = 12
annualInterestRate = 0.2
monthlyInterestRate = annualInterestRate / 12.0
monthlyPayment = 10
while (balance > 0):
balance = startBalance # reset the balance each iteration
print('checking monthly payment of',monthlyPayment)
for each in range(0, numMonths):
balance = balance - monthlyPayment
balance = balance + (monthlyInterestRate * balance)
# print('at month',each,'the balance is',balance)
# changed the indentation below
if (balance > 0):
monthlyPayment += 10
print('you should pay',monthlyPayment,'per month')
本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系:hwhale#tublm.com(使用前将#替换为@)