1

I need to calculate the minimum fixed monthly payment needed in order pay off a credit card balance within 12 months. The right answer should be 310, but I get 340. I was editing code for a few hours, yet didn't find any suitable solution. What is wrong here? How is it possible to fix it?

balance = 3329
annualInterestRate = 0.2
payment = 10

def year_balance(init_payment, init_balance):
    """ Calculates total debt after 12 months """
    interest_sum = 0
    for month in range(12):
        # balance after monthly payment
        unpaid = init_balance - init_payment 
        # monthly interest of remaining balance
        interest = unpaid * (annualInterestRate / 12.0) 
        interest_sum += interest
    yearly = init_balance + interest_sum # total debt after 12 months
    return yearly

total = year_balance(payment, balance) # total debt after 12 months

while total - payment * 12 > 0: 
# checks if payment is big enough to fully repay the credit after 12 months 
    payment += 10

print "Lowest payment: ", payment
NPE
  • 486,780
  • 108
  • 951
  • 1,012
Dovi
  • 15
  • 5
  • 1
    The code you provide gives an error- did you mean to change `interest_total = 0` to `interest_sum = 0`? – David Robinson Jan 14 '15 at 21:36
  • 1
    It looks like you're not calculating the total anew each time you adjust the payment. Since your code compounds interest monthly, you should recalculate the total each time the payment changes. – gankoji Jan 14 '15 at 21:36
  • 1
    @DavidRobinson Yes, I've revised the code – Dovi Jan 14 '15 at 21:40
  • 1
    There is a huge problem here, did you ever try to change the value of `payment` ? I did and everytime the result is same = `340`. So something wrong on your function logic. –  Jan 14 '15 at 21:42
  • 1
    @sacma That's only if you change the value of payment to something below 340. see the last few lines. – David Robinson Jan 14 '15 at 21:45

2 Answers2

2

You don't really need iteration to compute the monthly repayments. Instead, you could use the closed-form solution:

loan_amount = 3329
annual_interest_rate = 0.2
monthly_repayment = ((loan_amount * annual_interest_rate / 12.) /
                     (1 - (1 + annual_interest_rate / 12.) ** -12))
print monthly_repayment

This assumes monthly repayments and monthly compounding. For the general formulas see, for example, here.

NPE
  • 486,780
  • 108
  • 951
  • 1,012
0

We need to run the balance count function for each new payment amount, since the interest sum will be smaller for bigger payments. So

balance = 3329
annualInterestRate = 0.2
payment = 10


def year_balance(init_payment, init_balance):
    """ Calculates total debt after 12 months """
    interest_sum = 0
    unpaid = init_balance
    for month in range(12):
        # balance after monthly payment
        unpaid -= init_payment
        # monthly interest of remaining balance
        unpaid += unpaid * (annualInterestRate / 12.0)
    return unpaid

while year_balance(payment, balance) > 0:
    payment += 10

print("Lowest payment: ", payment)

Note: this is in case you make a payment before the credit period began. If you've done that after a month, you should first add monthly interest rate.

Andrew_Lvov
  • 4,621
  • 2
  • 25
  • 31