title: Heron method in Python
body: <p><a href="https://en.wikipedia.org/wiki/Heron&#39;s_method" rel="nofollow noreferrer">Heron's method</a> generates a sequence of numbers that represent better and better approximations for <em>√n</em>. The first number in the sequence is an arbitrary guess; every other number in the sequence is obtained from the previous number <em>prev</em> using the formula:</p>
  
  <pre><code>    (1/2)*(prev+n/prev)
  </code></pre>
  
  <p>I am supposed to write a function <code>heron()</code> that takes as input two numbers: <em>n</em> and <em>error</em>. The function should start with an initial guess of <em>1.0</em> for <em>√n</em> and then repeatedly generate better approximations until the difference (more precisely, the absolute value of the difference) between successive approximations is at most <em>error</em>.</p>
  
  <pre><code>    usage:
      &gt;&gt;&gt; heron(4.0, 0.5)
      2.05
      &gt;&gt;&gt; heron(4.0, 0.1)
      2.000609756097561
  </code></pre>
  
  <p>this is a bit tricky, but I will need to keep track of four variables:</p>
  
  <pre><code>    # n, error, prev and current
  </code></pre>
  
  <p>I will also need a <em>while loop</em> with the condition:</p>
  
  <pre><code>    ((current - prev) &gt; error):
  </code></pre>
  
  <p>A general rule for the <em>while</em> loop is that:</p>
  
  <pre><code>    # old current goes into new prev
  </code></pre>
  
  <p>So this is what I got so far, it's not much because to start with I don't know how to incorporate the <em>'if'</em> statement under the <em>while</em> loop.</p>
  
  <pre><code>def heron(n, error):
      guess = 1
      current = 1
      prev = 0
      while (current - prev) &gt; error:
          previous==1/2*(guess+n/guess):
             print (previous) # just a simple print statement
                              # in order to see what i have so far
  </code></pre>
  
  <p>Can someone give me a few pointers in the right direction please?</p>
  
  <p>thank you</p>