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

I think the problem is in the last else statement. Is there and indentation problem for the if, else statements?

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

Is_prime what's wrong?

This will return True or False on the first run through the loop, but you should only return True if x % n == 0 is False for all n. You need to move return True outside of the loop.


Aha, I knew it was indentation mistake! (but when I was revising my code before I put the "return True" in the same level as starting "if" statement). This time I returned it in the same level as "for" loop. Thank you :slight_smile:


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