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


#1


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:


#2

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

Error:

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 checking.like

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.


#3
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.


#4

thanks i’ll try fix this


#5

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.


#6

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.


#7

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


#8
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.


#9

But where does the 2 come in?


#10

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))
[2]
>>> 

#11

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?


#12

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].


#13

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


#14

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