is_prime please help!


#1


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

I thought the code would run properly but I think the error is in the for statement; but I can't figure it out.

Here is my code:

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


#2

If we pencil through the code, what happens when x = 0?

The range begins at 2, so nothing happens, to be exact. The function is exited with no return value, hence, None.

Python provides an easy out in this situation, else:, as in,

    for x ...
        if x ...

        else:

    else:
        return False

That's one consideration. You may wish to set up a test before the loop.