# Is_prime(2)

#1

I don't understand how the code is working for 2? 2 is divisible by both 2 and 1. so x%n is equal to zero in both cases. That should return False, but it is returning True

The code is working. And I know 2 is a prime number. I am just trying to figure out, how it is working for 2?

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

print(is_prime(2))``````

#2

It is because when you'll pass `2` as an argument then `for` loop will not run.

`for n in range (start,stop):`

start: Starting number of the sequence
stop: Generate numbers up to,but not including this number

#3

Put your for loop in an elif so that when you pass anything besides x < 2, the for loop can run.

``````if x < 2:
return False
elif x >= 2:
for loop
else:
return True``````

Hope this helps!

#4

Hi, this is my code if it helps

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

#5

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