is_prime failing on 9


#1

Hi,

The is_prime module is just not working for me! I am doing something stupid. Its failing when number = 9. Can any in suggest what is wrong in my code below.

def is_prime(x):
if x < 2:
    print x, "is not prime"
    return False
if x == 2:
    print x, "is prime"
    return True
for i in range(2,x):
    if (x % i) == 0:
        print x, "is not Prime"
        return False
        break
    else:
        print x, "is Prime"
        return True

is_prime(9)


#2

Figured out :smile:

The `else' statement was out of place.

def is_prime(x):
    if x < 2:
        print x, "is not prime"
        return False
    elif x == 2:
        print x, "is prime"
        return True
    else:
        for n in range(2,x):
            print "Testing if", x, "is divisible by", n
            if (x % n) == 0:
                print x, "is not Prime. It is divisible by", n
                return False 
        else:
            print x, "is Prime"
            return True

#3

try this

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