Prime check. What's happening?!


Oops, try again. Your function fails on is_prime(5). It returns None when it should return True.

What's happening?!!!!

Replace this line with your code.


At least if we had that we could attempt to run it. Hard to test a picture. Please post testable raw code. Thank you.


def is_prime(x):
    if x<2:
        print "No prime number is less than 2"
        return False
    elif x==2 or x==3:
        print "is a prime number"
        return True
    elif x>3:
        print "You have a valid call for prime number test!"
        for n in range(2,x):
            if (x%n)==0:
                print "is not a prime number"
                return False
        print "is a prime number"
        return True

x=int(raw_input("Enter the number you want to test for prime"))
print is_prime(x)


I agree..sorry about that. here's the code. FYI, I added the x==3 and changed it to x>3 due to errors i faced earlier.. otherwise i would have gone with x<2 ,x==2 and x>2


Since 3 is an odd number greater than 2 it requires no test of its own.

for n in range(2, x):

More on this later. Now consider that your code has an outer else that returns a True. Does this include the fall through of the loop? Answer: No. So the return value is None.

In a scenario where we have already excluded x < 2, study the above for loop parameters. What happens when x == 2? Answer: It is excluded so the loop never runs. It is the first fall through value. Any other that exist after the loop is completed are residual, and so return True as well. The real kicker is not being addicted to else. It has its uses, but it is not always needed.


thanks a ton ! removed the else and corrected indentation and it runs right!

But do bear with me a while longer.. just a silly doubt more to benefit my understanding:

elif x>2:
        for n in range(2,x):
            if x%n==0:
                print "is not a prime number"
                return False
       print "is a prime number"
       return True

supposing I call the function for x=3, the above condition will be evaluated right, and x%n==0 will not be satisfied in this case and wont the control go to the else and return true?


The highest value that is divided into 3 is 2 with MOD 1 resulting. Fail and fall through.

Scale back the if structures. We aren't fishing. There are definitive logical avenues.

if x < 2: return False
for n in range(2, x):
     # divisible is return False code segment
return True

No else or elif. Simple.

Notice x == 2 falls through, where x == 3 enters the loop.


Consider whether else is implicit without a need to case it out. Is the default to be implicit or explicit? else generally implies the latter. Fall through logic saves on code bloat and added logic. Something to think about moving forward.


thanks for the explanation! will definitely keep that in mind :smiley:


When you're done the track, take some time to explore the potential range values for this function. There is a bit to explore in that regard. I won't spoil it for you now.


I did!! just because of this program. It was interesting to note what range can do and what it cant


As long as we remember what range() is... A function. It returns an iterable, which is a defined list. There are a gazillion tricks we can do on a string or list with slicing. range() is a function that can generate such a list to begin iterating. Take the time to segue from this track to really get to know what this function is all about. Slicing is inbred. Like icing on a cake.


This topic was automatically closed 7 days after the last reply. New replies are no longer allowed.