I tried it with and without. I ended up going a really long about way of things… like adding an elif x % 3 == 0 return False and elif x == 5 return True, and then elif x % 5 == 0 return False…

I’m still not sure what was wrong with the original. I tried moving the else statement into different tab spaces and it kept giving me different errors.

i did smth like this but receive error. def is_prime(x): [tab]for n in range(2): [tab][tab]n < 2 [tab][tab]if x % n == 0: [tab][tab][tab]return False [tab][tab]else: [tab][tab]return True print is_prime(33)

i searched liitbe bit and found some code cheated liitle bit on it and passed challenge but i definitely dont understand whats going on. can some genius write comment to code so that i understand it? thanks

def is_prime(x): if x < 2: return False; if x % 2 == 0: return x == 2 y = 3 while y*y <= x: if x % y == 0: return False y += 2 return True print is_prime(4)

Cheating isn’t the best way to understand you how to it

def is_prime(x):
if x < 2:
return False;
if x % 2 == 0:
return x == 2
y = 3
while y*y <= x:
if x % y == 0:
return False
y += 2
return True
print is_prime(4)

def is_prime(x): #we create a function called prime with x as argument
if x < 2: # if x is smaller than two,
return False; #we return false because one and two is not prime
if x % 2 == 0: #if x is evenly divisible by 2
return x == 2 #we return true or false if x is equal to 2
y = 3 #we create a variable called y with 3 as value
while y*y <= x: #as long as the product of y times y is smaller or equal to x
if x % y == 0: #we test if x is evenly divisible by three
return False #if yes we return false because it's not prime
y += 2 #otherwise we add 2 to y variable
return True #if x is prime we return true
print is_prime(4) #we test if 4 is prime

This is the method I used also, but it was failing over and over until I realised that the final else: return True is an else statement for the for loop NOT an else statement for the final if statement… i.e. my indentation was causing it to fail but I couldn’t work out why.

def is_prime(x):
if x < 2:
return False
elif x == 2 or x == 3:
return True
elif x >= 4:
for n in range(2, x - 1):
print n
if x % n == 0:
return False
elif x % n != 0 and n == x - 2:
return True

I finally get result passed, however, I am a freshman.
Could you please help to check my code ?
I am not convinced about coding and I really have doubt about my code.

The following is my code:
def is_prime(x):
if x ==0 or x==1 :
return False
elif x == 2:
return True
elif x < 0:
return False
else:
for n in range(2,x+1):
print n
print x
print x%n
if x%n ==0 and x!=n:
return False
elif x%n !=0:
continue
elif n == x:
return True

Thank you everyone：）
Woah! The indent is actually correct in edit view while upload to the net, it seems indent disappears.
Well it is no matter and running result is pass on codecademy.

When I run my code I get the following error:
“Oops, try again. Your function fails on is_prime(-7). It returns True when it should return False.”.

Then I had to return False if the number is negative. The problem is the definition of prime number given in the exercise: it doesn’t apply just to positive numbers, but to negative as well. So -7 is actually a prime number.

Prime factorization of negative numbers cannot be done without fudging, which is possibly why the definition does not include numbers less than 2. If we want a prime to be negative, then multiply it by -1, however that number will not be a prime since it is not a product of 1 and a positive integer greater than 1.