I tried all day!!!!! I really cant get this one right. Any help is appreciated.
def is_prime(x): if x < 2: return False if x == 3 or x == 2: return True for i in range(2, x): if (x % i) == 0: return False return Trueprint is_prime(27)
Please read this post about markdown so your code/indent will show.
Seems your for loop is inside of your of your if statement:
if x == 3 or x == 2:
for i in range(2, x):
You might want to re-indent that.
You should have:for i in range(2, x-1)That is because otherwise it checks the number itself, and since x % x == 0, it will always return True
Its funny you say that because it worked with both versions : (2, x) & (2, x - 1)and i dont understand why
lol, didn't expect that xD
A post was split to a new topic: Any advise on reverse?
By 'it worked' does that mean it returned the correct result for all numbers?
yes it did. strange i know.
When the iterator is i, if i == x, x % i == 0 so should return False.
i == x
x % i == 0