Prime check. What's happening?!


#1



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.


#2

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


#3

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
    else:
        print "is a prime number"
        return True

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

#4

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


#5

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.


#6

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
else: 
       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?


#7

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.

Aside:

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.


#8

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


#9

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.


#10

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


#11

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.


#12

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