# Explanation requested for failure of else statement

#1

<Below this line, add a link to the EXACT exercise that you are stuck at.>
is_prime
<In what way does your code behave incorrectly? Include ALL error messages.>
Why does prime 2 give a return value of “none”?

I know that removing the last else and placing the “return True” under the “for” will make this work, but I don’t understand why the else statement doesn’t catch prime 2?
<What do you expect to happen instead?>

```python

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

``<do not remove the three backticks above>``

#2

This is because range works as an exclusive range. If you have `range(2,5)`, the numbers in this range are 2,3 and 4. So if you have `range(2,x)` and you substitute `x = 2`, you get `range(2,2)`, which is empty, so it will just skip the for loop in its entirety.

To fix this, you could change the range to `range(2, x+1)`, so you also take the number itself.

Now it will return an answer. You’ll have to check if this is the correct answer (hint: I don’t think so ). Try finding why it might not work precisely, if you can’t figure it out: feel free to ask.

#3

Thanks for the explanation nicoekkart and I understand why x+1 isn’t appropriate.
However, on further investigation I found 9 falls through to the else statement and gives a True return value, but is handled correctly when the else statement is removed as described in original post, now that really is confusing. I repeated the experiment 5 times as it didn’t make sense, and was sure it was must be my error, but the anomaly was confirmed.

So guess I’m asking why 9 is divisible by 3 in one instance but not the other??

Here’s the two outputs:
[robster@dos5 Code] python2 prime.py 9 False [robster@dos5 Code] python2 else_prime.py
9
True

#4

When the if statement does not return `False` it immediately returns `True`. This is the error. The loop needs to complete without returning False, and only then the function can return True.

#5

Thank you mtf, sorry for my stupidity…just having trouble with the logic of the loops. Once I can graphically visualise the process my 60+ year old brain should be fine

#6

You’re not alone, then. Seems I’m continually having to relearn…

#7

If you want to find these errors yourself, try simulating the code flow. Print statements can help here. If you for instance adapt the code to:

``````def is_prime(x):
if x <= 0:
print(x, " is smaller than 0")
return False
elif x == 1:
print(x, " is equal to 1")
return False
elif x > 1:
print(x, " is bigger than 1")
for n in range(2, x + 1):
print("checking divisibility of ", x, " by ", n)
if x % n == 0:
print(x, " is divisible by ", n)
return False
else:
print(x, " is not divisible by ", n)
return True
``````

If you run this code with 9, you get the following:

``````9  is bigger than 1
checking divisibility of  9  by  2
9  is not divisible by  2
True
``````

I which you can see that it doesn’t even check for 3. This means that there is a stop of the program, before it should stop, which means you are returning too early.