Its says "Your function fials on is_prime(9). It returns True when it should return False."

It should return False since when it evaluates 9%3 == 0 it's True.

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


however, the first condition tested by loop is: 9 % 2 == 0, which is false, causing else to run, which returns true, which will end the function (in order for the function to end, the loop will break)


Then, what can I do so it continues testing the rest of the options?
I can't remove the else there.


you could place return True outside the loop? This way, the whole loop will run (if its a prime number), and return True

if not a prime number, False will be returned which ends the function (end thus the loop)


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