is_prime


#1

def is_prime(x):
[tab]if x <= 1 or 2 or 3:
[tab][tab]return True
[tab]for n in range(2,x-1):
[tab]if x % n:
[tab][tab]return False
[tab]else:
[tab][tab]return True

This is my code, whenever I try and run it its gives me the error that 0 returns True instead of False. I have no idea what is wrong and have played with it for a while now. Please help.


Probleme avec est_premier
#2

Next time, please use markup:

https://discuss.codecademy.com/t/how-do-i-format-code-in-my-posts/28351

Anyway, the biggest problem your code has, is that your for loop will run only once. In the first run of the for loop, either if is evaluated to true, otherwise else will run. Either way, a return keyword is reached in the first run of the loop, the moment a return keyword is reached, the function ends (including the loop inside the function)

put the else at the same indent level as your for loop. Then it should work, why? If the number is not a prime, if would be true at some point, false is returned, and the function ends. Otherwise, all values are looped, and True gets returned (which is good, since the number is not a prime.

Also, in your if statement, check that the remainder is 0


#3

also, if you use or, you should compare everywhere:

if x <= 1 or x == 2 or x == 3:

you can't just do or, and put a bunch of values, you need to compare, again

And two and 3 are prime numbers, so you might want to change this completely


#4

Thank you once again!