What happens with my code, guys? I have already set the conditional statement that if x equal to 2, then the function returns True.In fact, it return None.Why is that?

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

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

Replace this line with your code.


but then you should use a or keyword, not and given x can't be 2 and 3


elif x == 2 or x == 3:


right, Our friend reply earlier


last 'else' is aligned with 'for', NOT WITH 'if'. Your last 'else' is aligned with 'if'. Mine is working fine. Good luck!


I 've tried but it still have a problem when it takes 9 as the input!


yes, @kabirsh answer covers this. A function ends (even if it is in the middle of a loop) when a return keyword is reached, which in your case is in the first run of the loop, you should change the indent of else/return True so it will run after the for loop is finished


Thanks you guys, it work !!