title: Python Bisection Search Logic Error- Returning Inaccurate Results
body: <p>I am trying to solve the third problem on this <a href="https://ocw.mit.edu/courses/electrical-engineering-and-computer-science/6-00sc-introduction-to-computer-science-and-programming-spring-2011/unit-1/lecture-4-machine-interpretation-of-a-program/MIT6_00SCS11_ps1.pdf" rel="nofollow noreferrer">MIT OCW assignment</a>. It requires that you calculate the monthly payment needed to clear a given debt in one year using the bisection search method. I am getting the expected output types but very inaccurate results. Can someone point out where I am going wrong? Thank you</p>
  
  <pre><code># Problem Set 1("C")
  # Time Spent: xx hours
  
  def payInOne_BisectionSearch (balance,annualRate):
      #initialize variables
      initialBalance = balance
      monthlyRate = float(annualRate/12.0)
      minMonthly = float(balance/12.0)
      maxMonthly = float((balance * (1 + monthlyRate ** 12.0 ))/12.0)
      monthlyPayment = float((minMonthly + maxMonthly)/2)
      numMonths = 1
      #define function to check balance after 12 months       
      def balanceAfterYear (balance, monthlyRate, monthlyPayment):
          for numMonths in range (1,13):
              interest = balance * monthlyRate
              balance += interest - monthlyPayment
              if balance &lt;= 0:
                    break
          return [balance, numMonths]
  
      while maxMonthly - minMonthly &gt;= .005:
          balance = initialBalance
          monthlyPayment = float((minMonthly + maxMonthly)/2)
          if balanceAfterYear(balance,monthlyRate,monthlyPayment)[0] &lt; 0:#paying too much
              maxMonthly = monthlyPayment
          elif balanceAfterYear(balance,monthlyRate,monthlyPayment)[0] &gt; 0:#paying too little
              minMonthly = monthlyPayment
          else:
              break
      print "Monthly payment to pay off debt in 1 year:", round(monthlyPayment,2)
      print "Number of months needed:", round(balanceAfterYear(balance,monthlyRate,monthlyPayment)[1], 2) 
      print "Balance:", round(balanceAfterYear(balance,monthlyRate,monthlyPayment)[0], 2) 
  
  payInOne_BisectionSearch (float(raw_input("Enter the outstanding balance")),float(raw_input("Enter annual rate as a decimal")))
  
  
      '''Test Case Expected:
         Enter the outstanding balance on your credit card: 320000
         Enter the annual credit card interest rate as a decimal: .2
         RESULT
         Monthly payment to pay off debt in 1 year: 29643.05
         Number of months needed: 12
         Balance: -0.1
  
         Test Case Actual Output:
         Enter the outstanding balance320000
         Enter annual rate as a decimal.2
         Monthly payment to pay off debt in 1 year: 26666.67
         Number of months needed: 12.0
         Balance: 39179.43'''
  </code></pre>