is_prime, about indent?


#1

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?


#2

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.


#3

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
    else:
        for n in (2, x-1):
            if x % n == 0:
                return False
        else:
            return True

#4

Should we be seeking a range limit?

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