0

i am doing the MITx 6.00.01x course and i am on the second problem set on the 3rd problem and i am stuck. my code:

    balance = 320000
    annualInterestRate = 0.2
    monthlyInterestRate = (annualInterestRate) / 12.0
    monthlyFixedPayment = 0
    empBalance = balance
    lowerBound = round((balance)/12,2)
    upperBound = (balance*(1+monthlyInterestRate)**12)/12
    monthlyFixedPayment = round( ( (lowerBound+upperBound)/2) ,2)
    while tempBalance != 0: 
        monthlyFixedPayment = round( ( (lowerBound+upperBound)/2) ,2)  
        for m in range(12) :
            tempBalance -= monthlyFixedPayment 
            tempBalance += (monthlyInterestRate)*(tempBalance)
            tempBalance = round(tempBalance,2) 
        if tempBalance > 0:
            lowerBound = round(monthlyFixedPayment,2)
            tempBalance = balance
        elif tempBalance < 0: 
            upperBound = round(monthlyFixedPayment,2)
            tempBalance = balance

    print('Lowest Payment: ' + str(round(monthlyFixedPayment,2)))

my code uses bisection search to generate the monthlyFixedPayment but after i get to the lines at the end that changes the upperBound or lowerBound values and then start the loop again, the lowerBound and upperBound values reset to their values to the ones outside the loop. does anyone knows how to prevent this?

jon smith
  • 73
  • 1
  • 6
  • 3
    Should `empBalance` be `tempBalance` in line 5? – BallpointBen Nov 08 '16 at 20:52
  • yeah that's not in the original code i accidently deleated it while copying the code – jon smith Nov 08 '16 at 20:55
  • Variables don't change their values by themselves. There's no way to prevent it because it doesn't happen. – Barmar Nov 08 '16 at 20:56
  • I can't reproduce the problem you describe: http://ideone.com/izgkqi. The script fails because the loop never ends. – Barmar Nov 08 '16 at 21:08
  • well i checked it and the upperBound value should be changed at the end of the first loop, it does and then it changes back to the value outside the loop. why does that happens? – jon smith Nov 08 '16 at 21:08
  • @Barmar well can you help me fix it? – jon smith Nov 08 '16 at 21:20
  • I can't help you fix something that doesn't happen. Please post the actual code. Don't retype it, because you make copying errors. Just use copy and paste. – Barmar Nov 08 '16 at 21:39
  • @Barmar i found the problem, when i use the round it transforms the number "29157.095" to the number "29157.1" but i need it to be "29157.09" do you know how to round it to that number? – jon smith Nov 08 '16 at 21:45
  • The actual value of the number is probably something like `29157.0950000001`, so it's closer to `29157.10` than `29157.09`. Floating point is approximate, so you have to be able to deal with inaccuracies like this. See http://stackoverflow.com/questions/588004/is-floating-point-math-broken – Barmar Nov 08 '16 at 21:48
  • That explains why the loop never ends, but it has nothing to do with the variables being reset to their initial values. – Barmar Nov 08 '16 at 21:49

0 Answers0