# is_prime

#1

I don't get why my code doesn't work, some help?

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

I get an error that it returns true when x is 0, but 0 < 2 so I don't get why it doesn't execute the if statement.

#2

Hello, @sathapoc!
I don't think your code returns True when the input is zero because of the condition in your if statement because 0 % anything is equal to zero or even when the input is two, the return will be False.
Try to first eliminate the numbers that are lower or equal to two, but know that the number two is a prime, and later loop to find cases where the input % N is equal to zero.

Quote me if there's anything else I can help you with!

Best regards,
g4be

#3

I know that 0 % anything is 0, but shouldn't the "or x < 2" take care of that since 0 is less than 2? Shouldn't it still execute?

#4

Since it's or you'll need, at least, one of the segments to be True so the information inside of it to be executed.
One question:
Is your else inside the loop instructio or outside?
Because the break statement will force the execution to exit the loop instruction and go straight to the else instruction, if the else statement is outside the loop instruction.

#5

But one of the segments is true - 0 IS less than 2.

Not really sure how to post my code as code, so you see the indention, but the else is at the same indention as the for loop. I thought that else statements after for loops executed only after the for loop was executed fully, and a break would stop it from being executed?

#6

To post your code do the following:

``[code]#Put your code here![/code]``

When the if instruction is executed, it jumps any other conditional instructions it find on its way.
It will jump to next instruction, outside the "for loop" because you used a break and forced the execution to leave the loop instruction, and will face with an else but since the if was executed, it will be ignored.
But if you add another if statement right after all of that, it can be executed if the condition on it is True.
Here's an example:

I'd suggest checking your indentation and see if everything's correct.
Quote me if there's anything else I can help you with!

Best regards,
g4be

Edit: I think this link will be helpful, if you ever have doubts about the conditionals statements.
Conditional Statements