pyschools square root approximation
<p>I am new to Python and stackoverflow. I have been trying to solve Pyschools 'while loop' example for square root approximation (Topic 5: Question 9). However I am unable to get the desired output. I am not sure if this issue is related to the loop or the formula. Here is the question:</p>

<blockquote>
  <p>Create a function that takes in a positive number and return 2
  integers such that the number is between the squares of the 2
  integers. It returns the same integer twice if the number is a square
  of an integer.</p>
  
  <hr>
</blockquote>

<p>Examples:</p>

<pre><code>sqApprox(2)
(1, 2)
sqApprox(4)
(2, 2)
sqApprox(5.1)
(2, 3)
</code></pre>

<p>Here is my code:</p>

<hr>

<pre><code>def sqApprox(num):
    i = 0
    minsq = 1                           # set lower bound
    maxsq = minsq                       # set upper bound
    while i*i&lt;=num:                     # set 'while' termination condition
        if i*i&lt;=num and i &gt;=minsq:  # complete inequality condition  
            minsq = i
        if i*i&lt;=num and i &lt;=maxsq:  # complete inequality condition
            maxsq = i
        i=i+1                       # update i so that 'while' will terminate
    return (minsq, maxsq)
</code></pre>

<hr>

<p>If I create this function <code>sqApprox(4)</code> and call it on IDE, I get output <code>(2, 0)</code>.</p>

<p>Could someone please let me know what I am doing wrong? Thanks in advance.</p>

<p>This is why your code does what it does:</p>

<p>After the line <code>maxsq = minsq</code> is executed, both of those values are 1.</p>

<p>When we come to the loop</p>

<pre><code>while i*i&lt;=num:                     # set 'while' termination condition
    if i*i&lt;=num and i &gt;=minsq:  # complete inequality condition  
        minsq = i
    if i*i&lt;=num and i &lt;=maxsq:  # complete inequality condition
        maxsq = i
    i=i+1                       # update i so that 'while' will terminate
</code></pre>

<p>first note that inside the loop <code>i*i&lt;=num</code>, so there is no need to retest it. Thus it is equivalent to:</p>

<pre><code> while i*i&lt;=num: 
     if i &gt;=minsq:  
         minsq = i
     if i &lt;=maxsq:
         maxsq = i
     i=i+1
</code></pre>

<p>In the first pass through the loop <code>i == 0</code> but <code>maxsq == 1</code>, making the second condition true, hence setting <code>maxsq</code> equal to the current value of <code>i</code>, which is 0. In subsequent passes through the loop, <code>i &lt;= maxsq</code> is false (since <code>maxsq == 0</code> but <code>i &gt; 0</code>) hence <code>maxsq</code> is never moved beyond 0. On the other hand, the first condition in the while loop keeps updating <code>minsq</code> as intended.</p>

<p>I would recommend forgetting about both <code>minsq</code> and <code>maxsq</code> completely. Have the loop simply be:</p>

<pre><code>while i*i &lt;= num:
    i += 1 #shortcut for i = i + 1
</code></pre>

<p>When the loop is done executing, a simple test involving <code>i-1</code> is enough to determine what to return.</p>

python