编写一个程序,计算所需的最低固定月付款,以便在12个月内偿还信用卡余额

时间:2012-10-16 03:17:07

标签: python python-2.7

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

在这个问题上,我们不会处理最低月付款率。

以下变量包含如下所述的值: 余额 - 信用卡上的未结余额 annualInterestRate - 年度小费率

该计划应打印出一行:每月支付的最低金额,以偿还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 ...我不知道如何得到这个......

9 个答案:

答案 0 :(得分:4)

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)

答案 1 :(得分: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

答案 2 :(得分: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)))

但是没有回复你正在寻找的答案。

答案 3 :(得分:1)

首先,您必须修复月份&lt; = 12 使它成为月份&lt; 12 其次,您使用的是此处不需要的表格  balance =((余额 - minimumMonthlyPayment)*(1 + monthlyInterestRate)) 因为只是你不必在这里使用最低付款 你的代码太糟糕了

试试这个:

    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

答案 4 :(得分: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

答案 5 :(得分: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

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

答案 6 :(得分: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

答案 7 :(得分: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

答案 8 :(得分:0)

for i in range(12):
     balance = balance - (balance * monthlyPaymentRate) + ((balance - (balance * monthlyPaymentRate)) * (annualInterestRate/12))

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

正确10/10