Is_prime help


#1

I am stuck on the problem is_prime.
Below is what I have and it throughs the error:
Your function fails on is_prime(3). It returns None when it should return True.

I am confused as to why this is. It seems to me that because it doesn't qualify in line 8, it should return True in the las line.

Could someone help me out?


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


#2

range(2, 2) is an empty range.

Further, if a loop always exits on its first iteration, then it's not much of a loop


#3

Well you're on the right track just a few extra lines and statements here and there.

For example,

You can do without this statement because after checking if x is = 0 or = 1 you can go straight into your for loop. And I realized if statements do not necessarily have to go along with an else. So you can just write your for loop as is like this.

 #Iterate from 2 to the argument given`
            for n in range(2, x):
                          if x % n == 0:
                            return False
            return True

Note how I have indented my return statement when it comes to for loops indentation is a major factor.