Unpossible
Unpossible

Reputation: 623

Code to Calculate Minimum Monthly Credit Card Repayment Over A year

Please I am trying to find out what is wrong with my reasoning, and hence my results. I am studying an online course where I am supposed to calculate the minimum figure needed to eliminate credit card debt over 12 months. I am given an annual interest rate, a value for the amount of debt (balance), and a value by which the monthly payments should increase (multiples of 10) only. From my reasoning, my code generated should iterate over the months, but if the balance value is not zero, it should increase the monthly payments and recalculate. My values are (I think) just slightly off of the expected results. My code is like this:

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

For balance = 3329, annual interest rate of 0.2, My result is: 310 (correct)

For balance = 4773, annual interest rate of 0.2, My result is: 380 (incorrect, should be 440)

For balance = 3926, annual interest rate of 0.2, My result is: 340 (incorrect, should be 360).

Please can someone help enlighten me where I am wrong?

Thanks!

Upvotes: 4

Views: 2756

Answers (4)

Mandar Parkhi
Mandar Parkhi

Reputation: 1

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))

Upvotes: 0

pirt
pirt

Reputation: 1213

You're almost there. There are a few problems in your implementation.

First off, you need to reset the balance after realizing the previously tested monthly payment didn't, well, pay out.

Secondly, the tabbing where you check the balance and increase it is wrong. As it stands, you pay 10 more dollars a month each month, which if I'm understanding your question properly is not what you want. You want to increase the monthly payment after seeing the one 10 dollars less didn't pay it off in 12 months.

Just as another point, your else: break is unnecessary as it will break out of the while loop when it enters into the next iteration.

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')

Upvotes: 2

Aziuth
Aziuth

Reputation: 3902

How about this:

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

What I essentially did was getting the if-condition out of the for-each, with a copy for balance used. while(running) essentially iterates over possible values for monthlyPayment.

(you could go with while(currentBalance > 0) if currentBalance is set earlier, but I'd go with my while(running) approach so it is read like a do-until-loop)

Upvotes: 1

Captain Giraffe
Captain Giraffe

Reputation: 14705

  1. The balance payed should be the same for the entire year so the if inside the for each doesn't make sense. The if isn't needed.

  2. The balance needs to be reset to the starting value when trying out a new monthly payment.

I tried it with these changes and it matches your test cases.

Upvotes: 1

Related Questions