Is_prime


#1



Hello, thanks in advance for your help. I am struggling with the is_prime exercise. They ask us to make a function that determines whether or not x is prime.

I am receiving this error message:
"Oops, try again. Your function fails on is_prime(4). It returns True when it should return False."

Before, I was receiving an error message about prime(3):
"Oops, try again. Your function fails on is_prime(3). It returns None when it should return True."
Not sure if it is valid but my internal reasoning for this error message was that when it reached my for loop and saw that my range was from 2 to 2 (range(2,x-1),range(2,3-1)), that it returned none...?


As of now, it seems to me that when checking is_prime(4), it should pass through the "if x < 2" and "elif x == 3" and stop at the "elif x > 3" where it should then start with n=2 (according to the defined range), find that 4/2 is in fact equal to an integer, and return "False." Could anyone clue me into why it returns "True" instead?

-Yelena


def is_prime(x):
    if x < 2:
        return False
    elif x == 3:
        return True
    elif x > 3:
        for n in range(2,x-1):
            if x / n == int:
                return False
            else:
                return True


#2

here:

if x / n == int:

two things, the result of x/n is an actual integer, where int is a data type.

an actual integer (for example 1) doesn't equal data type.

Secondly, why would you use this condition?


#3

Thank you, I've reconstructed the code considering what you've said and it is working now! Here is my new code.

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

#4