I am having a little trouble with the is_prime exercise (here). My code is the following:
def is_prime(x): if x > 2: for n in range(2, x-1): return x % n == 0 elif x == 2: return True else: return False
I get the error
Your function fails on is_prime(3). It returns None when it should return True.
I don´t understand why it does not pass 3 (I take this is what is what is happening for the loop to return
none). If I am not mistaken, what the loop should be doing would be:
- Verify 3 % (3-1) == 0, and therefore return True
- Verify 3 % 2 == 0, and therefore return True
I added the
elif statement because I thought the function might not work when 2 was passed (it would be trying to work with range
(2, 1), which I think would contradict the
if x > 2 statement.
In any case, the whole thing looks a bit off, so any clues as to how to move forward are appreciated.