is_prime


#1



Oops, try again.
Your function fails on is_prime(4). It returns True when it should return False.
Can some1 help me what i am doing wrong?


def is_prime(x):
    if x == 0 or x== 1 :
        return False
    elif x ==2  or x ==3 :
        return True
    else:
        for i in range(2,x):
            if x % i ==0:
                return True
            else:
                return False


#2

Do not return True inside the for loop. Return False and move else over to the left so it lines up with for and returns True.


#3

You need to change your range to 2 to x-1. when i iterates up to x, x%x will equal 0 thus returning true.

However even with that changed it doesn't work... shrug Maybe

This is my code:
def is_prime(x):
if x == 0 or x == 1 or x < 0:
return False
for n in range(2, x-1):
if x%n == 0:
return False
n+=1
else:
return True


#4

The for loop traps 2 and above if you don't itetate up to x. That means all we need to do is trap anything less than 2.

    if x < 2:
        return False

The for loop only needs to return False.

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

#5

I agree with your answer with the exception for the if statement. The if statement should read:

if x % n == 0:
return False


#6

My typo. Thanks for pointing that out. Fixed.