Perfect Square function that doesn't return
<p>I'm a beginner working with Python and I was given this task: <em>write a function which returns the highest perfect square which is less or equal to its parameter (a positive integer).</em></p>

<pre><code>def perfsq(n):

    x = 0
    xy = x * x
    if n &gt;= 0:
        while xy &lt; n:
            x += 1

        if xy != n:
            print (("%s is not a perfect square.") % (n))
            x -= 1
            print (("%s is the next highest perfect square.") % (xy))
        else:
            return(print(("%s is a perfect square of %s.") % (n, x)))
</code></pre>

<p>When I run the code to execute the function it doesn't output anything. I'll admit, I'm struggling and if you could give me some advice on how to fix this, I would be grateful.</p>

<p>I see your mistake, and it's an easy one for someone to make.  When you define</p>

<pre><code>xy = x*x
</code></pre>

<p>the computer calculates <code>x*x</code> and assigns that <em>number</em> to be the value of <code>xy</code>. So when you then add one to <code>x</code> it does not change the value of <code>xy</code>.  You have to tell the computer to recalculate <code>xy</code> every time:</p>

<pre><code>while xy &lt; n:
    x += 1
    xy = x*x
</code></pre>

python loops if-statement