Skip to content
Advertisement

Why is my class object not working as intended?

I have two issues that need resolving. I have created a class object LoanInstance. The object should create a list of numbers (int or float) that show different aspects of a personal loan repayment balance. There are 2 main parts to the object.

Part 1: Read in data from a data frame (I’ve created an example data frame below)

df = {'amount':[7000], 'term':[36], 'apr':[0.145], 'product':['standard'], 'score':[750], 'customer_type':[Home], 'Channel':[trad]}

Part 2: Generate the repayment profiles for the loan

Part 1 is working fine. However, part 2 is not working as I expect.

These are the steps:

Step 1: create a loan object

JavaScript

Step 2: read in data from pandas dataframe (this works fine)

JavaScript

Step 3:generating the balance profiles

balance profiles are generated with the following method applied to the loan object.

JavaScript

Issue 1!

This produces the error: TypeError: ‘int’ object is not callable.

It is being caused by the expression below:

monthly_repayment = self.amount/(12(1 - (1 + (self.apr/12))**-n*12))

What’s going wrong here?

Issue 2

When I bypass issue 1 by replacing the problematic expression with a number (i.e 200) I can get the code to run.

JavaScript

However, when I try to print any of the balance profile attributes they come up as an empty list [ ].

i.e running the code below comes out as an empty list [ ]

JavaScript

Why is this happening ? I expected a list of numbers represented the repayment balance for the loan taken into the object in step 1.

Thanks for your help in advance!

Full class code is here:

JavaScript

Advertisement

Answer

For the first issue,

Multiplication operator (*) is used to perform multiplication. In your case, self.amount/12(...) note the missing * operator between 12 and (. Because of the missing operator, it treats 12 as a callable (function). But 12 is an int and not a callable, hence the error. So insert the * if you mean multiplication. Also, I would suggest properly insert the brackets to avoid ambiguity. For example, in the case of **-n*12, you can change it to **(-n*12). If this is what it means.

For the second issue,

Note the return statement, return print(nominal_interest). A return statement is used to end the function right there and return the value where the function was called. Meaning, the function stops executing at this line, and all the further instruction are skipped. And since, by default balance_profile_start is [], it displays an empty list. You probably only want to print nominal_interest, instead of returning it. So the line becomes just: print(nominal_interest)

User contributions licensed under: CC BY-SA
3 People found this is helpful
Advertisement