0

I want to use bisection search to find out how much monthly payment should be in order to pay the full amount of balance within 12 months which user will input. However, this code I write goes into the infinite loop,showing "low, high, montlyPayment infinite times." I don't know which code causes this problem since conditional statement seems right to me .

initialBalance = float(raw_input('Enter the outstanding balance on your           credit card'))
annualInterestrate = float(raw_input('Enter the annual credit card  interest rate as a decimal'))
monthlyInterestrate = round(annualInterestrate, 2)

balance = initialBalance
while balance > 0:
    numMonth = 0
    balance = initialBalance
    low = balance/12.0
    high = (balance*(1+(annualInterestrate/12.0))**12.0)/12.0
    epsilon = 0.01
    monthlyPayment = round((high + low)/2.0, 2)
    while abs(monthlyPayment*12.0 - initialBalance) >= epsilon:
        print 'low =', low, 'high =', high, 'monthlyPayment =', round(monthlyPayment,2)
        if monthlyPayment*12.0 < balance:
            low = monthlyPayment
        else:
            high = monthlyPayment
        monthlyPayment = round((high + low)/2.0, 2)
        while balance > 0 and numMonth < 12:
            numMonth += 1
            interest = monthlyInterestrate * balance
            balance -= monthlyPayment
            balance += interest
balance = round(balance, 2)

print 'RESULT'
print 'monthly payment to pay off debt in 1 year:', monthlyPayment
print 'Number of months needed:', numMonth
print 'Balance:',balance
K.heer
  • 197
  • 1
  • 2
  • 10
  • Second line in your loop: `balance = initialBalance`. Is it by design, or by accident? – Ilja Everilä Jul 07 '16 at 11:27
  • 1
    I'm wondering what the point is of finding out an approximate result by trial and error, while you could reverse the formula and instantly get the exact result? – spectras Jul 07 '16 at 11:28
  • balance = initialBalance is redundant but I don't think that is the cause of this problem. This is a problem set I'm working on MIT Open courseware, which specifies the use of bisect search . – K.heer Jul 07 '16 at 11:38
  • Could you add the inputs causing the infinite loop? – Sergei Lebedev Jul 07 '16 at 12:23
  • These are the inputs: `Enter the outstanding balance on your credit card: 320000` `Enter the annual credit card interest rate as a decimal: .2 ` – K.heer Jul 07 '16 at 12:30
  • Use pdb to debug the program. http://stackoverflow.com/questions/1623039/python-debugging-tips – Jobin Jul 07 '16 at 12:40

1 Answers1

0

I have re-coded the above problem as

balance = 120000
annualInterestRate = 0.1
rate=annualInterestRate/12.0
high=(balance * (1 + rate)**12) / 12.0
low=balance/12.0
payment=0
bal_ref=balance
unpaid=balance
N=0
while (abs(unpaid) > .01):
    month=0
    pay=(high+low)/2
    balance=bal_ref
    while(month < 12):
        unpaid=balance-pay
        balance=unpaid + (unpaid * rate)
        month +=1
    if (abs(unpaid) < .01):
        payment=pay
        break
    elif (unpaid > .01):
        low=pay
    elif (unpaid < -.01):
        high=pay
    N+=1
print("Payment:",round(pay,2))
Souravi Sinha
  • 93
  • 1
  • 2
  • 11