2

这个问题适用于 python 2.7。问题要求编写一个程序,计算在 12 个月内还清信用卡余额所需的最低每月固定还款额。固定每月还款额是指一个每月不变的数字,而是一个常数每月支付的金额。

在这个问题中,我们不会处理最低每月支付率。

以下变量包含如下所述的值: balance - 信用卡上的未结余额

该程序应打印出一行:将在 1 年内偿还所有债务的最低每月付款。

假设按月末余额(当月还款后)按月复利。每月付款必须是 10 美元的倍数,并且所有月份都相同。请注意,使用此付款方案,余额可能会变为负数,这没关系。所需数学的摘要如下所示:

月利率=(年利率)/12 每月更新余额=(上一次余额-每月最低还款额)x(1+月利率)

我想出了一个问题的代码;但是,我反复遇到无限循环。

    b = balance = 3329
    air = annualInterestRate = 0.2
    monthlyInterestRate = (air/12)
    mmp = minimumMonthlyPayment = (balance * monthlyInterestRate)
    month = 0
    while month <= 12:
        b = ((b - mmp) * (1 + (air/12)))
        month = 1 + month
        if b <= 0 and month == 12:
           break
        elif b > 0 and month == 12:
           b = balance
           month = 0
           mmp = minimumMonthlyPayment + 10.00
    print str('Lowest Payment: ' + str(round(mmp, 2)))

有人可以帮我修复此代码吗?对于给定的余额,最低付款是 310 ......我不知道如何得到这个......

4

9 回答 9

5
monthlyInterestRate = annualInterestRate/12
monthlyPayment = 0
newbalance = balance
while newbalance > 0:
    monthlyPayment += 10
    newbalance = balance
    month = 1

    while month <= 12 and newbalance > 0:
        newbalance -= monthlyPayment
        interest = monthlyInterestRate * newbalance
        newbalance += interest
        month += 1
    newbalance = round(newbalance,2)
于 2014-09-05T12:25:46.093 回答
2
monthlyPayment = 0
monthlyInterestRate = annualInterestRate /12
newbalance = balance
month = 0

while newbalance > 0:
    monthlyPayment += 10
    newbalance = balance

    for month in range(1,13):
        newbalance -= monthlyPayment
        newbalance += monthlyInterestRate * newbalance
        month += 1
print " Lowest Payment:", monthlyPayment
于 2015-01-19T09:27:45.117 回答
1

First of all you have to fix month <= 12 make it month < 12 secondly you are using a form which is not needed here balance = ((balance - minimumMonthlyPayment) * (1 + monthlyInterestRate)) because simply you don't have to use the minimum payment here Your code is too bad

Try this:

    balance = 3329
    annualInterestRate = 0.2

    monthlyPayment = 10
    monthlyInterestRate = interestRate/12
    newbalance = balance - 10


    while newbalance > 0:
        monthlyPayment += 10
        newbalance = balance
        month = 0

        while month < 12 and newbalance > 0:

            month += 1
            interest = monthlyInterestRate * newbalance
            newbalance -= monthlyPayment
            newbalance += interest

    newbalance = round(newbalance,2)


    print " Lowest Payment:", monthlyPayment
于 2013-11-02T03:38:34.483 回答
1

这应该在所有情况下为您提供正确的答案:

monthlyPayment = 10
monthlyInterestRate = annualInterestRate /12
newbalance = balance - 10

while newbalance > 0:
    monthlyPayment += 10
    newbalance = balance
    month = 0

    while month < 12 and newbalance > 0:
        newbalance -= monthlyPayment
        interest = monthlyInterestRate * newbalance
        newbalance += interest
        month += 1
    newbalance = round(newbalance,2)
print " Lowest Payment:", monthlyPayment
于 2014-09-08T18:33:11.380 回答
1

我认为这是一种相当简单的方法:

x = 50
o = balance
a = [1,2,3,4,5,6,7,8,9,10,11,12]
monthly = annualInterestRate/12

while balance>0:
    for months in a:
        u = balance - x
        balance = u + (monthly * u)
    if balance > 0:
        x += 10
    else:
        break
print x
于 2015-01-21T23:02:58.010 回答
1
balance = 3329
annualInterestRate = 0.2
monthlyInterestRate = annualInterestRate/12
monthlyPayment = 0
newbalance = balance
while newbalance > 0:
monthlyPayment += 10
newbalance = balance
month = 1
while month <= 12 and newbalance > 0:
    newbalance -= monthlyPayment
    newbalance += (monthlyInterestRate * newbalance)
    month += 1    
print "Lowest Payment:",monthlyPayment

认为这将是您疑问的最佳解决方案..并满足答案

于 2014-09-10T11:55:48.887 回答
1

这段代码有点奇怪,即像这样的行:

   mmp = minimumMonthlyPayment = (balance * monthlyInterestRate)

用双等号。

此代码不会卡住:

balance = 5000
annualInterestRate = 0.2
monthlyInterestRate = (annualInterestRate/12)
minimumMonthlyPayment = (balance * monthlyInterestRate)
month = 0
while month <= 12:
    balance = ((balance - minimumMonthlyPayment) * (1 + monthlyInterestRate))
     month = 1 + month
    if balance <= 0 and month == 12:
        break
    elif balance > 0 and month == 12:
        month = 0
        minimumMonthlyPayment + 1.00

print str('Lowest Payment: ' + str(round(minimumMonthlyPayment, 2)))

但不会返回您正在寻找的答案。

于 2012-10-16T03:32:16.427 回答
1

这基本上是 randomizertech 答案的略微改进版本,它允许用户输入不同的变量,而不是仅针对初始余额和年利率的固定值进行。最后它会打印出更多对用户有用的变量。

InitialBalance = float(raw_input("Enter your current balance: "))
InterestRate = float(raw_input("Enter the yearly interest rate as a decimal: "))


monthlyPayment = 0
monthlyInterestRate = InterestRate/12
balance = InitialBalance


while balance > 0:
    monthlyPayment += 10
    balance = InitialBalance
    numMonths = 0

    while numMonths < 12 and balance > 0:

        numMonths += 1

        interest = monthlyInterestRate * balance

        balance -= monthlyPayment

        balance += interest

    balance = round(balance,2)

print "RESULT"
print "Monthly payment to pay off debt in 1 year: " , monthlyPayment
print "Number of months need to pay the debt: " , numMonths
print "Balance after debt is paid: " , balance
于 2015-12-12T20:02:00.223 回答
0
for i in range(12):
     balance = balance - (balance * monthlyPaymentRate) + ((balance - (balance * monthlyPaymentRate)) * (annualInterestRate/12))

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

正确 10/10

于 2018-07-31T02:53:53.740 回答