6. is_prime


#1

Hi!

I’m stuck on the exercise mentioned in the title.

I have written the following code:

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

and receive the following error message:

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

How is it returning None instead of False or True as I have defined in the function?

Thankful for your help!


#2

Error:

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

now look at your code if you call the function with 2 as an argument what does it return?
first it checks

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

so the if statement will not run.right?
next it’ll go to for loop

for n in range(2, 2):

simply this for loop will not run too

so the function will end now. so what is it returning?simply nothing( None )


#3

Thank you for replying!

I understand the error and have now corrected it. However, now I receive a new error message:

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

Your function fails on is_prime(3). It returns False when it should return True.


#4

I read the instructions more closely and came up with the following code:

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

The error message is now:

Your function fails on is_prime(4). It returns True when it should return False.

I don’t see how 4 would be True, as 4 % 2 is = 0. Any ideas?


#5

I am in a very similar situation as you. I fix one error and then the next one pops up. Just know you are not alone :slight_smile:
This is what I have and I get an error on 9

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


#6

I found another post discussing this, showing that the following code will work:

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

What I don’t see is how the above mentioned code will work, but not the following:

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

How is it that the return True statement cannot be a part of the inner if else statement?


#7

By the way, your post really helped me with the Digit_sum assignment and how to think about loops. Well done!


#9

Ohhhh one of my previous posts. Cool, I’m glad that helped!


#10

can you repost the second code in a way that shows the indentations too? I just figured mine out and as usual, the solution was ridiculous


#11

Done! Please share your solution as well!


#12

This is what worked. The last else statement had to be aligned with the “for” statement and I had it aligned with the “if” statement :face_with_raised_eyebrow:

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

#13

I ended up with the same way being correct. Do you understand why though, that it had to be outside the if statement?


#14

To be honest, no. I thought the if and else always need to be aligned.


#15

Hi @astridpersson and @christinele,

Usually, an else header is aligned with an if header or an elif header. However, it is also permissible for an else to be aligned with a for or while loop header. For examples and explanations of such cases in Codecademy exercises, see Loops: While / else and Loops: For / else.

For the Practice Makes Perfect: is_prime exercise, we can use a for / else structure where, if the loop completes all its iterations without being interrupted by the execution of a return statement, the else will execute. For a typical solution to this exercise, this would occur when no divisor was found because the number under consideration turned out to be a prime number.


#16

Thank you for your clarification! Would it be possible to choose in this case, to have the else header aligned with the if statement rather than the for header without error like in our first solutions?


#17

Hi @astridpersson,

The problem with this code …

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

… is that the if / else structure that is inside the for loop will return True as soon as an n is found that does not divide evenly into x. For example, in the case where x is 9, this happens when it is discovered that 2 does not divide evenly into that number. As a consequence, 9 is erroneously reported to be a prime number. This is because the loop does not have a chance to check 3 as a divisor, which does divide evenly into 9, and would prove that it is not prime.

In essence, the main issue is that we want the for loop to be able to check all potential divisors before we report back that none could be found that divides evenly into the candidate number. An else that matches a for loop is appropriate for such an approach.

There may be a way to redesign the logic of the code so that you can have a final else that aligns with an if header instead of aligning with a for loop, but that would likely involve code that is more complex than necessary.


#18

It makes sense. Thank you!


#19