# Is_prime

#1

Your function fails on is_prime(0). It returns None when it should return False.

return False

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

#2

Hi @jackalack117
Your source code looks fine, besides that you are only checking in the conditional statements if the 'n' var is divisible by 2. Remember, if a number is divisible by anything besides 1, it's not deemed a prime number.
So therefore, before that for loop add:

``````if x < 2:
return False``````

In the for loop:

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

I really hope this helps you!!

#3

It absolutely does help me.
Thank you for your help, and incredibly quick response. (wow)

#4

Hi,

Why is your 'return True' statement not included as an else statement?

#5

Thanks, already resolved with the first response.

#6

The reason why the 'return True' statement isn't included as an else statement is because it's more coherent to just write that line of code on the 'outside' of the for loop. It's the same as writing an else conditional since in this case, 'else' will be called if the loop doesn't work.

#7

@methodrunner22178 Great question!!!

#8

Thank you,

So, when the loop is completed, if the "if" statement does not return False, the function will return True. Normally, the function returns None by default, but we changed the return "value" to True. So for example, if x is 3 then the loop does not evaluate this to be false, and so the function returns its assigned True value by default. Is this correct?

#9

@methodrunner22178 You're welcome
And yes, that reasoning is indeed correct. Very intuitive!!! As I said, great question. Keep it up!

#10

why my code is wrong , just used to use a else statement, could your give me some idea? @methodrunner22178 @infinitycoder14 , thanks in adavance .

``````def is_prime(x):
if x < 2 :
# print 'bukeyi'
return False
elif x==2 or x ==3 :
return True
else:
for n in range(2,x-1):
print n
print x % n
if x % n == 0:
# print 'False'
return False
# break
else:
return True
#return True
is_prime(9)
``````

#11

i guess get the idea…
“the problem is that return ends the function” someone said.
is_prime should return when iteratoration ends, range(2,9) has many n in it. so, we just need the finally True returned , “return True” should be outside the if statement and under the for loop. my english so bad.

#12

Hey,

Awesome Question! Perhaps my editor is playing tricks on me, but you have a few indentation errors. Starting from the very first " if " statement, indent back one space, and indent the next return False statement back two spaces. Then indent the elif statement back one space and the next return true statement back two spaces. * I think that you need to go back and review indentation because it is a very important topic * Replace your first else statement with a more specific condition, such as: if x > 2. There are a few more indentation errors in your for loop, but the code is almost all right! The last issue is the last else statement…Else statements can lead to false positives so try to limit using them in this. * Your return True statement should be outside of your for loop * This is because if the loop is completed and does not return False, your function will return the default value of “None”. By placing the ‘return true’ outside of the for loop you change this default “None” to “True”. So, if the number is not prime, the loop will return False. If the number is prime, the loop won’t return False so your function returns True. Happy Coding!
View the code solution below

#13

Thank you for your help, and incredibly quick response:grinning:

#14

I think the range should be range(2,x)
For example:
print range(2,4) # priint [2,3]
My code here:
print range(2,4)
def is_prime(x):

``````if ( x<2) :
return False
for a in range(2,x):
if x%a==0 :
return False
else:
return True``````

#15

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