I can't figure out why my code is not working...

here is my code for is_prime() function:

def is_prime(x):
    if x < 2:
        return False
    elif x == 2:
        print True
        for i in range(2, x):
            if x%i == 0:
                print False
            if i == x-1:
                print True


After your 'for' loop add 'print i, x%i', and you'll see that the iteration goes only one circle till it gets its first True or False.


Those lines can be removed. as can these,

Inside the loop, have the if statement return False, and then as a last line, not inside the loop, return True.


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