15.6 - is_prime


#1

Can anyone tell me why the code below does not work? I've looked at other threads and got the answer for how to solve the problem but I still want to know why my code fails on is_prime(9)?

Thanks to anyone who can help me out!

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


#2

May be simplified to,

        for i in y:
            if x % i == 0:
                return False
        return True

#3

A post was split to a new topic: My code goes wrong. It returns the error