# What is wrong with this code?

#1

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

Oops, try again. Your function fails on is_prime(2). It returns None when it should return True.

What am I doing wrong? How could it possibly ever return none?

#2

None is Python's way of saying null or nothing was returned. I would add an elif that checks if
x == 2, then return True.

#3

The clue is in the error message. Add a conditional to test for 2 in the input, and return True.

#4

Okay, now it's failing on is_prime(3). It returns false again. I'm aware of what none means, I just don't know why it's throwing it out.

#5

Consider what happens when the input is 3...

for n in range(3, 2):

Loop does not execute. Remember, the last index in the range is not evaluated. Try this...

for n in range(2, x):

Also, consider where the last True is written. As an else statement inside the for loop, when it should be an else statement on the for loop.

def is_prime(x):
if x==2:
return True
if x<2 or x%2==0:
return False

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

print is_prime(2)     # True
print is_prime(3)     # True