# 6. is_prime Troubleshooting Help

#1

Please review and help troubleshoot

Here's the code:

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

#2

First, test your for loop. If you write "for n in range(2,x-1)" and print out the "n", what will the console prints out? Maybe test with number instead "x-1". Then you know what you have to write.

Second, if "x" is greater than 2, always x-1 will define your return. If x = 9, the for loop will test number 8 last and it will return true, but 9 is not prime.

Think about it.

#3

It says that it returns None

#4

When x == 9, n reaches 7, not 8.

``for n in range(2, x - 1):``

`x - 1` needs to be 9 in order to reach 8. Remember `range()` does not include the upper bound.

``````    a = range(2, 9)
print a           # [2, 3, 4, 5, 6, 7, 8]
a = range(2, 9 - 1)
print a           # [2, 3, 4, 5, 6, 7]``````

The code is valid as written, and follows the instructions. The real issue is,

``````else:
return True``````

The above is inside the if statement so returns on the first iteration in that loop that does not return False.

#5

That is why i wrote first that he have to check what will prints out for "n" if the range is (2,x-1). but u wrote it down

#6

Still, not the issue. We need to zero in on immediate concerns if we are to be of any help.

#9

You should use: for n in range(2,x)
because range(a,b) function ends at b-1, so if x is 3 and u use x-1, range function will ends on 1 which is even smaller than 2.
Besides that, you should put 'return True' out of if/else statement.

#10

A post was split to a new topic: My code is that can you help me

#11

This is not what the instructions ask for. The range is not the problem, as has been stated repeatedly in this topic. It's the placement of the final `else` statement that is the problem.

• Define a function called is_prime that takes a number x as input.
• For each number n from 2 to x - 1, test if x is evenly divisible by n.
• If it is, return False.
• If none of them are, then return True.

The ambiguity here is that we don't know if the author means literally, x -1 or figuratively, since a `range(2, x)` it technically 2 through x - 1.

At any length, it won't matter. So long as we do not iterate up to and including `x` we'll be fine.

There are other limits that we could impose, but that is not what the exercise asks for. We should try to complete it according to instructions, then practice writing different approaches on our own.

Consider, what if the limit is only one-half of `x`, plus 1 for good measure?

What if the limit is square-root of `x`, plus 1?

We rarely see problems with the range, but we most frequently see problems with indentation.

#12

Thank you for your reply. I agree in most cases range is not a problem, but I was just taking about the matter the warning shows, you can see in the screenshot the warning is the program fails in execute is_prime(3), and it returns None.

If the problem comes from the indent of last line, it will return False or True instead of None. (Indeed it's also another problem)

Range(2,x-1) works well with numbers greater than three, but here the website will check if 3 is feasible for this function.

#13

When x is 3,

``for n in range(2,2):``

does not execute. This is where the `else` in line with `for` will catch it and return True.

``````for n in range(2, x-1):

else:
return True``````

or more simply

``return True``

No `else` needed.

``````    if x < 2: return False
for n in range(2, x):           # the 2 catches all even numbers for x.
if x % n == 0: return False
return True``````

Now picture x being 3. It won't enter the loop because the range is too small. Anything that makes it past the loop (and the first condition) will always be True, hence, Prime.

#16