is_prime, about indent?


above is my code.
could anyone tell me why first "else" cant be as indent as its if statement?
I peek other's code is like mine. but only change decrease indent?


Examine the first example. What happens when x%n is not 0? The function immediately returns True, which may very likely be false. Not a reliable function.

Examine the second example. What happens when x%n is not 0? Nothing. The loop continues without interruption. At the end of the loop, the else statement is as much as saying, You're still around, so here you go... and returns True by default. This else is tied to the for not the if.


I tried fixing the indent as suggested, yet somehow is_prime(9) is still not going through, help

def is_prime(x):
    if x == 2:
        return True
    elif x == 1 or x == 0:
        return False
        for n in (2, x-1):
            if x % n == 0:
                return False
            return True


Should we be seeking a range limit?

for n in range(2, x-1):