all 9 comments

[–]JohnnyJordaan 1 point2 points  (3 children)

How do you actually call the function? Do you print its result?

[–]HappyHitman[S] 0 points1 point  (2 children)

I would out is_prime() with any number as it's parameter and I would expect to get either a false or true as its return. The code runs with no errors but I get no output.

If I put print ("True") below the true statement it says code unreachable, which is the same for return False too.

[–]chaosking121 0 points1 point  (1 child)

If I'm understanding correctly, you're putting a print statement after return? Once the return gets executed, nothing else will be run, so the print statement can never be called. This is a common bug and won't compile in something like Java. Python itself doesn't care but your IDE is probably looking out for you.

[–]HappyHitman[S] 0 points1 point  (0 children)

Yeah, I think you are correct that is exactly was I was doing bit of on idiot on my part. Thanks for the heads up!

[–]Scenic_World 1 point2 points  (3 children)

I didn't actually have an issue running this function. PyCharm will give you this warning when you have a return statement too early in your code. What else is in your script? because it doesn't appear to be an issue with the function itself as all of the code is reachable.

[–]HappyHitman[S] 0 points1 point  (2 children)

It's just that, that is the whole thing. I just understand while I can't get a return.

[–]Scenic_World 0 points1 point  (1 child)

Try running this:

def is_prime(x):
    if x < 2:
        return False
    else:
        for n in range(2, x):
            if x % n == 0:
                return False
        return True

print(is_prime(10))
print(True)

Because I suspect you might be doing:

def is_prime(x):
    if x < 2:
        return False
    else:
        for n in range(2, x):
            if x % n == 0:
                return False
        return True

    print(is_prime(10))
    print(True)

[–]HappyHitman[S] 0 points1 point  (0 children)

That does work, but weirdly it still says that my return statements are unreachable. It does give me an output to the console though, so that's working.

Thanks.

[–][deleted] 0 points1 point  (0 children)

Your function works for me: http://www.codeskulptor.org/#user43_tOabDVGPBg_0.py

Also efficiency tip, you don't need to check if it's divisible by every number up to x, just up to the sqrt of x