6. is_prime


#1

Down below is my code. It says that 9 returned True when it should've returned false. If I add an exception and say that if x == 9 return False, it goes until 15 and says the same thing. It returns True when it should've been False. I can't see how it's coming up with that. When it does 9%3 is it not getting 0?? It does this for 9, 15, 21, and 25 and most likely more if I kept going.


def is_prime(x):
    r = range(1,(x+1))
    if x == 0 or x == 1:
        return False
    if x == 2:
        return True
    else:
        for n in (r):
            while n > 1:
                if x%n == 0:
                    return False
                else:
                    return True


#2

A function ends, the moment a return keyword is reached. Even in a loop, so your loop ends at the first run, since it encounters a return keyword.

I would put the return True or else at the same indent level as the for loop, so your entire loop can run instead of getting stuck in the first run