Root-finding by iterated bisection Both of the following code give the same result.  But I'm not sure where should I put the raise statement.

def bisection(f, start, stop, eps = 1e-5, max_iteration = 100):
  for __ in range(max_iteration):
    half = start + (stop - start)/2
    if abs(f(half)) &lt; eps or half - start &lt; eps:
      return half 
    if f(half) * f(start) &gt; 0:
      start = half
    else:
      stop = half
  else:
    raise ValueError('Cannot find root in the given range')


Or

def bisection(f, start, stop, eps = 1e-5, max_iteration = 100):
  for __ in range(max_iteration):
    half = start + (stop - start)/2
    if abs(f(half)) &lt; eps or half - start &lt; eps:
      return half 
    if f(half) * f(start) &gt; 0:
      start = half
    else:
      stop = half

  raise ValueError('Cannot find root in the given range')


P.S.  I asked this question Stack Overflow and someone there suggested to ask it here.
Bug

I see no justification for or half - start &lt; eps.  With that, you no longer guarantee that the return value is a root of f.