0

I'm having a bit of trouble with a problem from an online course (Introductory Python). Essentially, we are told to use binary search to find the lowest fixed payment each month to clear out a debt in a year (rounded to the nearest $0.01), when given a balance and an annual interest rate. My solution, when uploaded to their online grader, gives me only this error:

"There was a problem running your solution. We couldn't run your solution."

Am I possibly in an infinite loop? If so, I don't quite see how. The original code is posted below. Thank you all for taking the time to read this!

MonthlyInterestRate = annualInterestRate/12
month = 1
LB = balance/12
UB = balance*(2.7/12)
check = balance
while abs(balance) > 10:
    payment = (LB + UB)/2
    while month <= 12:
        balance = (balance - payment)*(1 + MonthlyInterestRate)
        month = month + 1
    if balance > 10:
        LB = payment
        balance = check
    elif balance < -10:
        UB = payment
        balance = check
    else:
        print('Lowest Payment: ' + str(payment))
        break
4
  • 4
    Is this the full code? You haven't defined annualInterestRate or balance anywhere. Commented Oct 15, 2012 at 21:54
  • 2
    The server doesn't give you the full traceback? What errors do you get when you run it locally on your computer? Commented Oct 15, 2012 at 22:02
  • 1. try "hello world" example on the online grader to make sure you handle input/output correctly. 2. test your solution locally on your machine. 3. Encapsulate the interest rate logic in a separate object (to test the binary search and the balance calculations separately e.g., MonthlyInterestRate = annualInterestRate/12 is wrong if there is a compounding) 4. Use integers or decimal.Decimal for arithmetic on money. Commented Oct 15, 2012 at 22:19
  • annualInterestRate and balance are given to us by the online grader, so they provide values when they check. I don't have errors when I run it locally, which is the odd part... Thanks for your help though! Commented Oct 15, 2012 at 22:23

1 Answer 1

2

It seems that the likely culprit is that you never reset the value of month after the inner while loop. That is, once inner has executed once it will never execute again. This means that the value of balance will not change and you will be stuck in an infinite loop.

Since you're only using month to iterate a set number of times you should really change the inner loop to

for m in range(month):
    balance = (balance - payment)*(1 + MonthlyInterestRate)

edit:

Scratch that, just playing around with your function it seems that it quickly converges on a some if the starting balance is greater than 13. If balance is less than 10 then your function decreases balance so it definitely doesn't terminate. If the balance is less than 13 then it seems to terminate in 1 iteration.

And I tested the function with different annual interest rates and it makes absolutely no difference to the outcome. The convergent number seems to be approximately 90% of the start balance. This is one borked function.

Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.