PRACTICE MAKES PERFECT-is_prime-6/15(Been stuck on this for ages)


This is what i’ve managed to do so far but I still can’t seem to figure out how to complete the task. I also don’t fully understand how to do it so any help would be great.
Thanks :smiley:


At first understand the error message then try to slove it.


your function fails on is_prime(2). It returns None when it should return True.

It means if you call the is_prime function with 2 as an argument then it’ll return None.

so you can call the function with print statement for

print is_prime(2)

so now how does it work?

Lets go through it

First it’ll check the if statement

if 2<2: # as x=2
  return Flase

so it’ll not run because it’s Flase.right?
then it’ll go to for loop.

for n in range(2,2)

It’ll not run too.i believe you know how for loop works.

now the function will be end .so it returns nothing(None)

now consider how you can solve the error.

elif x%n != 0:
    return True

This is the same as writing an else, which is still a problem. Your code sends false positives for any number greater than 2. When x is 2 there is no return value so the function returns None.

Remove the elif and slide the return True line to the left so it is at the end of the function block.


thanks i’ll try fix this


I didn’t know the answer was that simple! If u don’t mind can u explain why it works now? I still don’t understand fully even though the program works.


The first line of the function rejects any number less than 2 since we know that primes are positive integers greater than 1.

if x < 2: return False

The next step is to iterate through all numbers greater than 2:

for n in range(2, x):

The above will not run when x is 2. We let the value slip past the loop where it runs into the last line,

return True

since we know that 2 is a prime number.

The loop conditional rejects any number that is divisible by 2 or more.

 if x % n == 0: return False

Once the loop is complete, the value will drop down to the last line and return True. Since it is not divisible, it must be prime.


I see but what do you mean by “The loop conditional rejects any number that is divisible by 2 or more.” :confused:

if x % n == 0: return False

is a conditional expression statement. When x is divisible by n (meaning there is no remainder) then it cannot be a prime number. Prime numbers are only divisible by themself and 1.


But where does the 2 come in?


In the for loop:

for n in range(2, x):

When x is 2, the range is empty so the loop does not run. When x is any other number, it will fail the conditional on the first pass if it is even since any number divisible by 2 is even, and cannot be a prime.

>>> list(range(2,2))
>>> list(range(2,3))


I think i understand it. So when 2 runs through the program and returns True does it replace the variable n with 2? Then does if x%n==0 become _if x%2==0 which then returns False for every value in the range that is divisible by 2?


n does not get initialized when x is 2 since there is no range to set it with. The value 2 never gets inside the loop (when x is 2) so the code inside the loop never runs. Only when a value greater than 2 is given to the loop does n take on values from 2 to x - 1. Note the range when x is 3. [2].


I think I’ve forgotten how everything works so I’m going to revisit the previous lessons :confused:
Appreciated the help though :smiley: thanks!


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