# Your function fails on is_prime(4)

#1

I get an error that says “Your function fails on is_prime(4). It returns True when it should return False.” Could someone explain why? My code is below. I tried working it out and I don’t see how this doesn’t work as a solution. Thank you.

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

Why does is_prime fail for some numbers?
#2

and here are two reasons why…

and

#3

Because of this condition

``````if x >= 2:
return True
``````

4 >=2 so function returns false.

You should change the code like this

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

#4

I tried your solution. It return True with is_prime(9). This is the same problem with my code. Must be some issue with if else statement in the for loop. If I remove the else in for n in range(2, x-1) and make return True at the same indentation as the for loop, then it would be alright. Wonder whats the problem here?

#5

Yes i corrected the code now . please try now

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

print is_prime(9)
``````

#6

Yes it worked now. But can you help me find out what is wrong with my code? It also show 9 to be prime number.
At first thought, I thought it is because of the else in the for loop that overwritten the first result. But when I tested it with 12 and 14, it gave right answer. The numbers that it got wrong is 9-15-21-27,33,…

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

#7

I played around with your code a little and I think you should incorporate some condition for capturing inputs greater than 2. If you add an if condition before the for loop then it seems to work. I modified your code below and it works.

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

#8

if we insert a print statement in your code:

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

print is_prime(9)
``````

we see that your loop is only making a single iteration (`print n` only prints once)

this because in the first iteration of your loop, `x` is divisible by 2, if so return True else return False. the return keyword will end the function

this is not desired behavior, it means all uneven numbers are marked as prime number, given they are not divisible by 2.

@kmonroe25 also made another critical change which made the code to work, and its not `if x >= 2`

#9

Thank you, it did worked. But I think the code that changed it is the else keyword that have the same indentation level as the for keyword.

#10

yep, spot on nguyen. But do you also understand why the change of this indent is so important?

#11

It’s because changing else indentation would make the for loop to iterate through every number in the range(2,x) before returning the result and end the loop. Right?

Before fixing the issue. The code would just test if a number is divisible by 2, if it’s not than the code would return a True without testing if the number is divisible by 3-4-5-6,…etc

#12

That is true for a prime number.

for numbers which are not a prime number, the if condition will evaluate to true (at some point within the loop), which means the function will return false

`for/else` works like this: if the loop can make all its iterations (thus loop condition becomes false), the else clause is executed

but when you `break` the loop, the else clause won’t get executed

in your code, you use `return` to “break” the loop (once a return keyword is reached, the function ends right away given return is the last thing a function does)

so you could even get rid of `else` keyword altogether. Just ensure that `return True` is at the right indent level:

``````for loop:
# some code in the loop

return True
``````

#13

Thank you for your help! Didn’t really understand about for loop till now. I did not even know about returning result would end the loop.

#14

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