# Is_prime returns None

#1

I've been working on is_prime for a while now, and I don't understand what is wrong with my code. It tells me that is_prime(2) returns None instead of False.

I made special cases for all x below 2, and used a while loop to check if x divided by some number n is zero or not. There are return statements and everything. What is structurally wrong with this code that makes it not return True or False?

``````def is_prime(x):
if x <= 1:
return False
else:
n = 2
while n < x:
if x%n==0:
return False
break
else:
n += 1
return True``````

#2

`None` represents lack of value, a function that doesn't explicitly return something will return `None` to say that it had no return value.

Can you think of a way that could happen for your function?
You could start by verifying that `None` is the result of `is_prime(2)` as the error message claims.

#3

I fixed it by adding another special case for 2, and, interestingly enough, changing the while loop to a for loop. shrugs Go figure.

#4

Yes. Do! There's something to learn there.

And, a very important thing to notice in your code is that your while loop always returns on the first iteration, it isn't a loop at all.

#5

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

The last for loop is unnecessary, because the code will loop through all of the numbers between 2 and x. That means you only need to return true only if x can't evenly divide the numbers in between.
e.g: if x is 19, and we start at 2, so is 2 evenly divisible by 19? 3? 4?... so if all of the numbers from 2-18 can't be divided evenly, we will return True.

#6