# Is_prime

#1

My code does not work for -7. if anyone could either guide me in the fight direction, or share a working code, thank would be much appreciated. I learn beest from seeing other people's work

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

#2

`You won't need all of that to complete this. Use my code:`

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

Not that hard

#3

Awesome, thank you. That was very helpfulâ€‹

#4

No problem

#5

Why is return True outside of the for loop?

This is what I have so far:

def is_prime(x):
if x < 2:
return False
elif x == 2:
return True
else:
for n in range(2, x):
print n
if x % n == 0:
return False
else:
return True

#6

Hm... your code doesn't work with me. If you are talking about my code, the `return True` is outside the for loop it is because, if all of the if statements don't work then it returns true.

Sorry, I explained it so complicated. It really is!

#7

Sorry, I should have stated that my code does not work. But thank you for your explanation - it makes sense now. Cheers.

#8

No problem, happy to help.

#9

This topic was automatically closed 7 days after the last reply. New replies are no longer allowed.