6. is_prime exercise returns none error



My code returns the following error:

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

I expect it to work. I cannot see why it doesn't work. I have consulted other similar topics, haven't found the answer there. A nudge in the right direction would be great. Otherwise, I fear I could stare at it forever and not figure it out.

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


A function ends the moment a return keyword is reached, if this happens in a loop, the loop simply breaks. When should you return True? And does your code currently does this?


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