Is_prime


#1

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
    else:
        for i in range(2, x):
            if x%i == 0:
                print False
                break
            if i == x-1:
                print True

#2

Hi,
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.


#3

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.


#4

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