# 6. is_prime

#1
``````here is my code:

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

I get error:
Oops, try again.
Your function fails on is_prime(9). It returns True when it should return False.

How to fix it!??

#2

At the end of your code, you have;

for n in range (2, x):
if (x % n) == 0:
return False
else:
return True
the else statement should be for the entire for loop, not the if statement. Try lining up the else with the for. That should work

#3

That's great! Thank you! But why it needed to line up?

#4

I was asking the same question, but i think this is what causes the problem:

If you indent the else, it will iterate the for loop over the if statement AND when your if statement is false it will automaticly return true after each number, because thats what you assigned.
It accuelly should check all the numbers in range (2, x) first and then, after all the numbers are checked without a single match, return true.

#5

That makes sense. I just understand it that if you indent the else, it would line up with if. Then, it would return True, after just checking one thing. If you want it to check all the numbers, and then call it True, you would want to line up the else with the for, so that after it checked all the possible numbers, if it didn't already call it False, it would call it True.