# Is_prime Python question

#1

Hi,

here’s my code:

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

That’s the error message:

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

I do not understand what’s wrong with the code.

Can you explain me?

CR

#2
``````range(params)

params:
inclusive start index, (optional; zero if not present)
exclusive end index,   (required)
step (stride length)   (optional; +1 if not specified)``````

#3

So do you want to say that my input for range() is not correct.
Is it wrong that I put x in range()?
Were it correct if I did range(int(x), … )?

#4

Correct.

``````range(start_index, end_index, stride_length)
``````

in that order. Your range should be,

``````range(2, x)
``````

Ignore the example in the instructions. It’s been discussed to death how it is an oversight of the author.

When x is the first argument, then the start_index is x, and where you have 2 written, that is the end_index. We would only write the range in this manner if we have a negative stride length.

``````x = 10

range(x, 2, -1)    =>  [10, 9, 8, 7, 6, 5, 4, 3]

range(2, x)        =>  [2, 3, 4, 5, 6, 7, 8, 9]``````

#5

Yay, now it works!

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

is_prime(4)
is_prime(18)
is_prime(11)
is_prime(3)
``````

And the output is:
False
False
True
True

Thanks a lot!

#6

`n` will never equal to `x` since `x` is not in the range.

``````if x % n == 0: return False
``````

There should not be an else clause on the if statement. What happens if we run a list of odd numbers through your function?

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

>>> for k in [3,5,7,9,11,13,15,17,19]:
print (is_prime(k))

True
True
True
True  # ???
True
True
True  # ???
True
True
>>>
``````

That can’t be right. We know that 9 and 15 are not prime numbers. This error is as a result of the `else` clause.

1. check if x < 2, and return False
2. loop over range(2, x), if x > 2
3. return True AFTER the loop

#7

Like this?:

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

for k in [3,5,7,9,11,13,15,17,19]:
print (is_prime(k))
``````

This is the result:

False
False
False
False
False
False
False
False
False
False
False
False
False
False
False
False
False
False

I does not work either, unfortunately .

#8

If we examine the range when x is 2,

``````range(2, 2)  => []
``````

Only when x is greater than 2 will there be a range, so we do not need to test that condition.

Recall that a non-prime number is factorable, meaning it is divisible. That is what this line is doing, testing divisibility…

``````if x % n == 0:
``````

When the modulo is zero, the number IS divisible, so not a prime. The return should be False.

To repeat, `n` will never equal to `x` so this conditional is superfluous and has no meaning.

Also, do not print inside your function, just return.

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

Note that the last line is not inside the loop. If that line is reached, x is either 2 (a prime), or the loop has finished and x is a prime.

#9

That’s right. I was not focused, it seems.

This is my new code:

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

for k in [3,5,7,9,11,13,15,17,18,19]:
print (is_prime(k))
``````

And here’s the output:

True
True
True
True
True
True
True
True
False
True

I dont know why it is not working. (9 => False)

#10

Remember we mentioned that `else` is not needed in that if statement? It’s kicking x out of the loop (out of the function, actually) and giving a false positive.

We must also rule out immediately any non-primes by definitition, a.k.a., less than 2.

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

That, too, does not have an `else` clause.

``````if ...:
for ...:
if ...:
return False
return True
``````

Notice that only the even number in the list returns False? That’s because all the odds are defaulted to the else clause, `return True`. While all primes other than 2 are odd, not all odd numbers are primes since many odd numbers are multiples of one or more numbers.

#11

Now I understand.

I removed the else clause.

Here’s the code:

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

for k in [3,5,7,9,11,13,15,17,18,19]:
print (is_prime(k))
``````

Here’s the output:

None
None
None
False
None
None
False
None
False
None

The non-prime number returns False whereas the prime number returns None.
Since I don’t have an else-clause, how can I return True?

#12

mtf shows you how to return True:

``````if ...:
for ...:
if ...:
return False
return True
``````

of course you could use `else`, but then it would be `for/else`, we only want to return True after the loop finished running (given the number is a prime number). But like mtf shows, then return True can also simple be placed outside/after the loop

#13

Oh yes, stupid me!
It worked well. The _None_s are replaced by _True_s now.

Thank you both for your help!

#14

Which case requires a `break` condition in theloop. As we can see there is no need for a break, so need for for…else.

#15

There is still one conditional missing. Run these test values…

``````for k in [-3,-1, 0, 1, 2]:
print (is_prime(k))
``````

What is the output?

#16

True for every item.

Do we assume that prime numbers are only positive?
If so, I would try this:

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

for k in [3,5,7,9,11,13,15,17,18,19]:
print (is_prime(k))

for k in [-3,-1, 0, 1, 2, -9]:
print (is_prime(k))
``````

Output:

``````True
True
True
False
True
True
False
True
False
True
False
False
False
False
True
False
``````

#17

No assuming. It’s by definition. Prime numbers are always positive, and always odd, save for 2.

Your code is now complete within the parameters of this exercise.

#18

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