【问题标题】:Code to Calculate Minimum Monthly Credit Card Repayment Over A year计算一年内最低每月信用卡还款额的代码
【发布时间】:2016-05-06 02:38:13
【问题描述】:

请我试图找出我的推理出了什么问题,因此我的结果。 我正在学习一门在线课程,我应该计算在 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)。

请有人帮忙指点一下我哪里错了?

谢谢!

【问题讨论】:

  • My result is: 360 (incorrect, should be 360). ?
  • 不应该 if(balance > 0) 与 for each 处于同一级别,而不是在 for each 内部?
  • 当我这样做时,我得到了一个无限循环,@Aziuth。
  • @OverlordQ,抱歉错字,已修复。
  • @Sina:但是你想要做的是迭代monthlyPayment的可能值,对吧?所以应该是while(monthlyPayment不足):重置余额,测试monthlyPayment是否正确,如果不正确则增加monthlyPayment

标签: python


【解决方案1】:

你快到了。您的实施存在一些问题。

首先,您需要在意识到之前测试的每月付款没有支付后重置余额。

其次,您检查余额并增加余额的选项卡是错误的。就目前而言,您每月多付 10 美元,如果我正确理解您的问题,这不是您想要的。 看到少了 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')

【讨论】:

  • 他的休息对于他的结果与正确答案一样接近是必要的。如果没有,他会有一个更明显的错误。
  • 哈,不错。正如您所指出的,整个if 语句在while 循环内是多余的,无论如何,如果monthlyPayment += 10monthlyPayment 开头,初始化为0。我保留它以明确原始代码是每月增加 10 次付款,而不仅仅是在之前的测试失败时。
【解决方案2】:
  1. 全年支付的余额应该相同,所以for each 中的if 没有意义。不需要if

  2. 尝试新的每月付款时,余额需要重置为初始值。

我对这些更改进行了尝试,它与您的测试用例匹配。

【讨论】:

  • 请问我该怎么做?我不确定我是否正确理解您。
  • @新浪pirt给了你完整的答案。不过,我会完全摆脱 if 。初始化 monthlyPayment = 0 并将 monthlyPayment+=10 移动到 while 内部。 if 是多余的。
【解决方案3】:

这个怎么样:

annualInterestRate = 0.2
monthlyInterestRate = annualInterestRate / 12.0
monthlyPayment = 10
running = true;

while (running):

    currentBalance = balance

    for each in range(0, 12):
        currentBalance = currentBalance - monthlyPayment
        currentBalance = currentBalance + (monthlyInterestRate * currentBalance)
    if (currentBalance > 0):
        monthlyPayment += 10
    else:
        running = false

print monthlyPayment

我所做的基本上是从 for-each 中取出 if 条件,并使用一个用于 balance 的副本。 while(running) 本质上是迭代monthlyPayment 的可能值。

(如果 currentBalance 设置得更早,你可以使用 while(currentBalance > 0),但我会使用我的 while(running) 方法,所以它读起来就像一个 do-until-loop)

【讨论】:

  • 非常感谢 Aziuth。你的 cmets 和你的代码真的帮助了我。
【解决方案4】:
Outstanding = 59400 # Total Outstanding Amount
interestrate = 4.2  # Interest Rate per month+ GST

#print("Month", "\t", "Interest+GST", "\t\t", "MinAmtDue", "\t\t\t", "Balance")
#print("-----", "\t\t", "-------", "\t\t", "--------", "\t\t\t", "-------")

month = 0
totpmt = 0
interest_GST = 0
minamtdue = 0
outstandingamt1 = Outstanding
while (outstandingamt1 + interest_GST - minamtdue) > 0 :
    month += 1
    interest_GST = outstandingamt1*4.2/100
    minamtdue = outstandingamt1 * 5/100
    #minamtdue = 12000
    outstandingamt1 = outstandingamt1 + interest_GST - minamtdue
    #print(month, "\t\t\t", round(interest_GST,2), "\t\t", round(minamtdue,2), "\t\t\t", round(outstandingamt1,2))
    totpmt = totpmt + minamtdue
#print(month+1, "\t\t\t", 0, "\t\t", round(outstandingamt1,2), "\t\t\t", 0)
print("Total Amount to be paid in ", month+1, "months= ", round(totpmt+outstandingamt1 , 2))

【讨论】:

    猜你喜欢
    • 2017-12-06
    • 2012-10-06
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2017-02-03
    • 2013-07-02
    • 1970-01-01
    相关资源
    最近更新 更多