15.6 is_prime error message



In the error box is says:
Oops, try again.
Your function fails on is_prime(0). It returns True when it should return False.

I do not know what is going wrong. I'd expect it to go through numbers 2 to the number before x, then check those numbers if they are divisible by i (The changing number).
I would appreciate help!

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


you will need to make a special condition (use if before the loop) to check for number which can't be catches by the loop.

lets say the value is 0:

for i in range(2, 0):

by default, steps are +1, but zero is already smaller then 2, so nothing is happening. so instead else executes, but zero is not a prime number

add the if condition for number smaller then 2 (this will also catch all negative numbers, which are also not considered prime numbers)


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