I am having trouble creating a class to model loans with interest and payments

140 Views Asked by At

I am getting having errors with the math in my loan repayment calculator. I am trying to create a class where I can create a loan, iterate it by month or year, and also apply payments to the loan. I am not entirely sure where my issue lies in this code.

I've tried altering the payment method and am stuck with the root problem with the math. I think there is an underlying issue with the way that I have interest accruing and the principle balance

class loan():

    p_i = 0 #initial principle
    p_new = 0 #total balance
    rate = 0 #interest rate (in %)
    acc = 0 #total accrued interest

    def __init__(self,p,rate):

        self.p_i = p
        self.p_new = p
        self.rate = rate

    def month(self):

        if self.p_i == self.p_new:
            self.acc += self.p_i * (1 + (self.rate/100)/12)
            self.p_new = self.p_i + self.acc
        else:
            self.acc += self.p_i * (1 + (self.rate/100)/12)
            self.p_new = self.p_i + self.acc

    def year(self):

        if self.p_i == self.p_new:
            self.acc += self.p_i * (1 + (self.rate/100)) - self.p_i
            self.p_new = self.p_i + self.acc 
        else:
            temp = 0
            temp = self.p_i * (1 + (self.rate/100)) - self.p_i
            self.p_new += temp
            self.acc += temp

    def payment(self,amount):

        temp = 0
        if amount < self.acc:
            self.acc -= amount
        else:
            self.p_i -= amount - self.acc
            self.acc = 0
            self.p_new -= amount

I would expect the output for say, a 10000 dollar loan with a 5% interest rate to be 10500 and then 11025 and the accrued interest to be 1025, instead I am getting 11000 and 1000 respectively.

1

There are 1 best solutions below

0
AirSquid On

I would recommend as a start that you take this out of a class and just do this procedurally until you get the math worked out. It seems you are battling the class syntax a bit.

So just try setting variables and some functions and then looping through some options.

Start with something like this:

principal = 10000
rate = 0.05

def monthly_interest():
    global principal, rate
    principal += rate/12*principal

def make_payment(payment):
    global principal
    principal -= payment


for month in range(1,13):
    monthly_interest()
    # print totals
    print('month {:2d}, principal ${:0.2f}'.format(month, principal))

Realize this is kinda ugly with global variables and all, but if you are having trouble getting the math to work, start here and then build to a class later when you have the kinks worked out.

Output:

month  1, principal $10041.67
month  2, principal $10083.51
month  3, principal $10125.52
month  4, principal $10167.71
month  5, principal $10210.08
month  6, principal $10252.62
month  7, principal $10295.34
month  8, principal $10338.24
month  9, principal $10381.31
month 10, principal $10424.57
month 11, principal $10468.00
month 12, principal $10511.62