# ６ is_prime

#1

This is my code.

I got the message."Oops, try again. Your function fails on is_prime(9). It returns True when it should return False."
But I cannot understand why x=9 is True."

Please tell me in simple English!!

#2

I think you should use a for loop instead of while loop. Like this :

``````def is_prime(x):
if x<2:                  #prime numbers except 0,1
return False
elif x==2:
return True
else:
for n in range(2,x-1): #in (2,12) we have 12-1%2 is not zero remainder hence 12 not prime
if x%n==0:
return False
else:
return True``````

#3

Your code is easy to understand! You are great!

Incidentally, I want you to explain why this while loop is wrong.
Please tell me , if you can !

#4

I am keeping this topic up by posting!

#6

If you really want to use while loop it should looks like that:

``````n = 2
while x % n != 0 and n!=x:
n+=1
else:
if n==x:
return True
else:
return False``````

Why? Because return works like break

#7

``````def is_prime(x):
if x<2:
print "Not a prime number"
return False
elif x==2:
return True
elif x==3:
return True
else:
for n in range(2,x-1):
if(x%n==0):
return False
break
else:
return True
print "Is this number prime?: " + str(is_prime(9))``````

#8

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