Python: Why does return not actually return anything that I can see
<p>I have the following code:</p>

<pre><code>def is_prime(n):
    limit = (n**0.5) + 1
    q = 2
    p = 1
    while p != 0 and q &lt; limit:
        p = n % q
        q = q + 1
        if p == 0 and n != 2:
            return 'false'
        else:
            return 'true'
</code></pre>

<p>But when I send in an integer, there is nothing returned. The console simply moves on to a new command line. What's wrong here?</p>

<p><strong>EDIT 1:</strong>
The following are screenshots of different scenarios. I would like to make it such that I call the function with a particular number and the function will return 'true' or 'false' depending on the primality of the number sent into the function. I guess I don't really understand the <em>return</em> function very well.</p>

<p>Also, note that when I send in to test 9, it returns true, despite 9 being quite definitely a composite number...should the if/else bits be outside the while loop?</p>

<p>Key to below image:</p>

<p>1: this is the code as it is above and how I call it in the Spyder console</p>

<p>2: adding a print statement outside the function</p>

<p>3: this is a simple factorial function offered by the professor</p>

<p><a href="http://i.stack.imgur.com/LX4hy.jpg" rel="nofollow">image here</a></p>

<p><strong>EDIT 2:</strong></p>

<p>I made a quick change to the structure of the code. I don't really understand why this made it work, but putting the if/else statements outside the while loop made things result in expected true/false outputs</p>

<pre><code>def is_prime(n):
    limit = (n**0.5)+1
    q=2
    p=1
    while p!=0 and q&lt;limit:
        p = n%q
        q = q+1
    if p==0 and n!=2:
        return 'false'
    else:
        return 'true'
</code></pre>

<p>Also, I call the function in the console using is_prime(int_of_choice)</p>

<p>Thanks for the helpful suggestions</p>

<p>Nothing is wrong, but you have to print out the return of your function.
Like this:</p>

<pre><code>def Test():
    if True:
        return "Hi"

print(Test())
</code></pre>

<p>In this case python will show "Hi" in your console.</p>

python python-2.7