Is_prime


#1

https://www.codecademy.com/en/courses/learn-python/lessons/practice-makes-perfect/exercises/isprime

i dont understand why is doesnt work. why it gets stuck on (9):
Your function fails on is_prime(9). It returns True when it should return False.



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 True
      else:
        return False




#2

You have to remember that your for loop is producing a result every time it iterates, so in your code as soon as x%n is zero it returns True meaning it’s a prime, in your code as soon as n = 2 on the first iteration the function returns true.

What you need to think about is using a for with an else statement, if the condition for x%n is never zero before the range runs out then it will go to the else statement and return True, you won’t need to add a break here as the function will return False before it goes onto the else statement. Hopefully this helps !


#3

There are a few issues. Let’s walk through them.

  1. The range.

Look at your range. Here’s the instructions:

For each number n from 2 to x - 1, test if x is evenly divisible by n.

  1. In the for loop, you want to check if the number is evenly divisible. And if no, return False. Think about that one. Look at your code. You are checking that it’s not evenly divisible and then return True.

  2. Returning too early.

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

Look at the else code block above. We want to loop through all of the numbers in that range. If the conditional expression if x % n == 0: is True, that means it’s an even number. We want to stop the loop and return False.

But we want to loop through all of the numbers until:

  • we find an even number
  • or we finish the loop

If we finish the loop, then return True.


#4

thanks for your answer
i changed it a bit, but still not working…

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

	

gets the error Your function fails on is_prime(3). It returns None when it should return True.

i dont understand why .Its suppose to be in the first condition...

#5

ok now i took out the - else: return True from the loop
it doesnt give an error anymore, it opened the next level but it doesnt say i was right. i think thats a bug already…


#6
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

    # You made it this far.
    # Look at the instructions.
    # What should you return here.

If the checks made it to end of the loop and it’s not an even number, what should you return?


#7

yes. i changed it already and returned True outside the loop. now it just return nothing. Not an error but it also doesnt say its ok. it just open the option to move to the next part. i am not sure whats going on…;


#8

You can run your own test to make sure it works by doing this:

print is_prime(9)
print is_prime(7)
print is_prime(2)

I submitted a bug report asking why there’s not a positive indication when you get it right, i.e. to alert you to move forward.


#9

\yes i did that with few numbers. seems like it works,
i will report a bug
thank you!


#10

You’re welcome. Good job.


#11

to @hellofromtonya

can I ask you one mam?

so here’s your solution for Talya’s problem

inside the else block you put

for n in range(2, x-1):
if x % n == 0:
return False
# you return False here if the condition is true (if x % n == 0)
#or else return True outside the loop
return True

“”“but what if the condition is met(if x % n == 0)
and because you put (return True) outside the loop
would (return True) still return that value or ignore it ? “””

im just confuse on that part please tell me, thank you :slight_smile:


#12

I was only showing the loop portion of the solution. Here’s the remaining part of it.

def is_prime(x):
    if x < 2:
        return False

    elif x == 2:
        return True

    for n in range(2, x - 1):
        if x % n == 0:
            return False

    return True

The checks are:

  1. Is it less than 2? If yes, then that number is not prime. Return False.
  2. Is it equal to 2? If yes, then 2 is a prime. Return true.
  3. If we made it this far, then we need to check the number set from 2 up to the number -1. Loop through each number to check if x % n == 0. If yes, then it’s not prime; return False.
  4. If we made through all of the checks, then it’s a prime number. We can return True.

Does that make sense?


#13

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