Is Prime


When i run this code it gives me an error message of Oops, try again. Your function fails on is_prime(1). It returns True when it should return False.

Shouldn't 1 be a prime number? Not sure what i can do to fix this. But would appreciate some help.

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


A prime number (or a prime) is a natural number greater than 1 that has no positive divisors other than 1 and itself.

So your error message tell you a god thing. Prime number must be greater then 1.
And if x < 2 and x != 0 is not a good way for finding a prime number as you can see a definition of prime number on the beginning.


when i ran it without the x != 0 it errored out on zero.


Your first line is one the right track, apart from the and x != 0 expression mentioned above. Any number less than 2 cannot be prime.

if number < 2:
    return False
if number > 2:
    # code
        return False

# number == 2 or survives the loop
return True


Should not be inside the loop. See early post code skeleton.


def is_prime(x):
    if x < 1 or x == 1:
       return False
    if x > 2: 
        if x % 2 == 0: 
            return False
    return True

This is my new code that works till i hit numbers like 9 that have a remainder. Not sure how to program in a check for remainders. Any suggestion?


You still need a for loop:

for n in range(2, x):
    if x % n == 0:
        return False


Ok thanks i got it now


Keep it simple...

if x < 2: