Member Avatar

Hi I'm quite new to Python and am trying to figure out where I'm going wrong, I'm trying to use a bisect search to find :-

Monthly payment made to clear loan amount

using :-

Monthly interest rate = (Annual interest rate) / 12
Monthly payment lower bound = Balance / 12
Monthly payment upper bound = (Balance x (1 + Monthly interest rate)12) / 12

at the moment I have :-

balance = 6758
annualInterestRate = 0.20
monthlyRate = annualInterestRate/12
numGuesses = 0
lo = balance/12
hi = (balance)*((1+monthlyRate)**12)/12
monthPay = (hi + lo)/2.0
NuBalance = balance
while abs((NuBalance)*(1+monthlyRate))-(monthPay) >= 0.01:
    print('low = ' + str(lo) + ' high = ' + str(hi) + ' MonthPay = ' + str(monthPay))
    numGuesses += 1
    if ((NuBalance)*(1+monthlyRate))-(monthPay) <= 0.01:
        print('Month Pay LO = ' + str(monthPay))
        lo = monthPay
    else:
        print('Month Pay HI = ' + str(monthPay))
        hi = monthPay
    monthPay = (hi + lo)/2.0
print('numGuesses = ' + str(numGuesses))
print('Month Pay = ' + str(monthPay))

Any help to where I'm going wrong would be appreciated.

Cheers

JP

Hello, fellow MITx student, your balance decreases when you pay loan, doesn't it? I do not want to help more.

Member Avatar

Thanks I've managed to work it out. :)