6. is_prime


#1



https://www.codecademy.com/en/courses/python-intermediate-en-rCQKw/0/6?curriculum_id=4f89dab3d788890003000096#


Oops, try again. Your function fails on is_prime(0). It returns None when it should return False.


No idea what is wrong with this code. If you can just please fix the mistake and tell me why it was wrong.

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


#2

I see several mistakes.

Lets start by the one you are currently facing, None is returned by default. So, for x=0 no return keyword is reached, maybe you can write a condition to check for values lesser then 2? Given 1, 0 and negative numbers can't be prime numbers


#3

I changed the code:
def is_prime(x):
if x < 2:
return False
for n in range(2, (x-1)):
if (x % n) == 0:
return False
elif (n / 1) == n:
return False
else:
return True

Now it returns: Oops, try again. Your function fails on is_prime(2). It returns None when it should return True.


#4

OK, I got it. The flaw in my logic was mainly due to not understanding properly range() function.


#5