Is_prime


#1

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

I am getting the following error message: Oops, try again. Your function fails on is_prime(0). It returns None when it should return False.

prime(0) should return False



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



#2

Error:

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

All numbers less than 2 are not prime numbers. so you can check whether or not the argument you pass less than 2.If it’s then returns False.

and here

 if x % n == 0:
    return True

If x is evenly divisible by n then it should return False.It’s not the prime number.


#3

Thank you. I changed my code to:

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

But got the following error message: **Oops, try again. Your function fails on is_prime(2). It returns None when it should return True.


#4

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