What am I doing wrong here?


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


a function ends the moment a return keyword is reached

so for x=9, the first case tested by the loop is n=2, lets see how that works out:

if 9 % 2 == 0:

this condition is false, so else runs, True is returned, the function ends

But for x to be a prime number, x shouldn't be divisible by any value in range of n

So, we only want to return True after the loop has finished running


7 posts were split to a new topic: Is_prime


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