I get an error - and i'm correct with my punctuations

56 views Asked by At
## Loan problem

def mon_pay( prin, an_i, dur ) :
    n = dur * 12
    r = (an_i/100)/12
    numerator = r*(1+r)**n
    dinominator = (1+r)**n - 1
    if an_i == 0 :
        mon_pay = prin/n
    else :
        mon_pay = prin * (numerator/dinominator)
    return mon_pay

def rem_pay( prin, an_i, dur, num_pay) :
    n = (dur * 12)
    r = (an_i/100)/12
    numerator = ((1+r)**n - (1+r)**num_pay)
    dinominator = ((1+r)**n - 1)
    if an_i == 0 :
        rem_pay = (prin * (1-num_pay/n))
    else :
        rem_pay = (prin * (numerator/dinominator))
    return rem_pay

prin = float ( input ("Enter the Principal of loan: "))
an_i = float ( input ("Enter the annual interest rate: "))
dur = int (input ("Enter the duration of loan: "))
mon_pay = mon_pay(prin, an_i, dur)
print("LOAN AMOUNT:",prin,"INTEREST RATE(PERCENT):",an_i)
print("DURATION(YEARS):",dur,"MONTHLY PAYMENT:",int(mon_pay))

for yr in range (1, dur+1) :
    total_pay = mon_pay*12*yr
    _yr = yr*12
    rem_pay = rem_pay(prin, an_i, dur, _yr)
    print("YEAR:",yr,"BALANCE:",rem_pay//1,"TOTAL PAYMENT",total_pay//1)

i have given my code above, it's a simple problem to calculate loan details( i am just just studying python and this is an assignment). When i run it i get this:

Enter the Principal of loan: 1000
Enter the annual interest rate: 10
Enter the duration of loan: 5
LOAN AMOUNT: 1000.0 INTEREST RATE(PERCENT): 10.0
DURATION(YEARS): 5 MONTHLY PAYMENT: 21
YEAR: 1 BALANCE: 837.0 TOTAL PAYMENT 254.0
Traceback (most recent call last):
  File "C:/Python34/Zsample9(loan_prob).py", line 35, in <module>
    rem_pay = rem_pay(prin, an_i, dur, _yr)
TypeError: 'float' object is not callable

the function

rem_pay = rem_pay(prin, an_i, dur, _yr)

runs the first time, but the second time it gives the mentioned error I can't see why, Anybody please help!

2

There are 2 answers

0
Joshi Sravan Kumar On

Don't use same name for variable and function.

In this case rem_pay is used both as function and float variable.

3
Mike JS Choi On

You are assigning a float result to the variable rem_pay the first time you run the function.

And it just happens so that rem_pay is the name of the function you are executing in order to get the float result.

So instead of assigning a float result to the function rem_pay, you should consider replacing the name the variable

payment_remainder = rem_pay(prin, an_i, dur, _yr)
print("YEAR:",yr,"BALANCE:",payment_remainder //1,"TOTAL PAYMENT",total_pay//1)

Notice rem_pay has been changed to payment_remainder.