Hi all, I'm working through the is_prime exercise in the python module. I'm confused as to why this code isn't working when calling is_prime(9). The error I'm getting says the code is returning 'True' instead of 'False'. I've followed it through from start to finish, and I can't for the life understand why it would return 'True'.
I've added comments as I went through, specifically to track what was happening with '9' as the argument. I'm at a loss here folks and it's very frustrating!
def is_prime(x): if x < 2: return False elif x == 2 or x == 3: return True else: # For numbers 2, 3, 4, 5, 6, 7, 8 for n in range(2, x): # if 9 is divisible by any number above, return false (not prime) and end loop if x % n == 0: return False break else: return True
Now obviously, 9 is divisible by 3, so at that point the code should be returning 'False'. If anyone could explain why this is returning True when calling is_prime(9), I would be very grateful indeed!