I read a lot of discussion threads before getting code that worked for is_prime. I had been getting an error for is_prime(0) returning None, is_prime(2) returning True (or None), etc.. I had been reading about inserting a lot of manual conditions (if x < 2:, if x == 1:, etc.) and using range (2, x) instead of range (2, x - 1), which I didn't always understand and which didn't follow the instructions.
I used the code below which worked, and I can see two things done differently here than what I had been trying: (1) The successful code does not contain an "else" corresponding to the "if n % x == 0:" statement and (2) the "return True" statement is outside any if-loop. Why does this work?
def is_prime(x): if x < 2: return False else: for n in range(2, x - 1): if x % n == 0: return False return True