# Is_prime

#1

The logic is correct, yet for input 2, it would return None in the answer instead of True. What's the problem?

I expected it would run properly, but why isn't it?
UPDATE: the code below is now fine..

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

#2

Yeah, I found it weird when I did this. I just created an if statement just for 2.
I think it's a codeacademy bug.

#3

Okay thanks, so what about now, how do I pass it?

#4

Could you explain to me why you put the range as `range(2,x/2)` in your loop statement?
Also, push the `return True` back 1 indent so it lines up with the loop so at least you are returning True if you exit the loop if the number is a prime.

#5

UPDATE
`def is_prime(x): if x<2: return False else: for n in range(2,(x/2)+1): if (x%n) == 0: return False return Trueprint is_prime(input())`

`range(2,(x/2)+1)` is because if suppose we take any number, let's say 11, make it half i.e 5.5, now if you think there can be no number bigger than 5.5 that could completely divide 11 and give remainder 0. Now because we want a whole number here, the value that is picked up is 5
Earlier I did `range(2,x/2)` and that was wrong because in that case the `range()` would pick number up till 4 for checking the divisibility of 11 which is below the half of it and hence incomplete check. `range(2,x)` or `range(2,x+1)` are just wastage of computing.
Thanks for helping, removing one indent of `return True` worked...

#6

Interesting. I will keep that in mind!

#7

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