6. Is_prime


#1

Hi guys,

I am trying to solve this problem. But it says that the code fails on 3 as it return "false" here is my code ... any suggestions?

def is_prime (x):
    if x ==0:
        return False
    if x<2:
        return False 
    elif x == 2:
        return True 
    else:
        for n in range (2,x):
            division = float (x/n)
            if round(division,0)-division>0:
                return True
            else:
                return False

#2

the problem is that in the first run of your loop, a return keyword is reached, and the moment a return keyword is reached the function ends.

You should change this, you should return false if the remainder (i don't understand why you not simple use the modulo (%) operator) is zero, and after loop is finished running (change the indent of else) return True


#3

Hi,

Thank you for your fast response. I am sorry, I didn't quite understand, where should I change the indentation?

Kind regards
Tania


#4

you should first swap return True and return False

You understand that with your current code, in the first run of the loop a return keyword is reached which causes the function to end? So what you want, is to return false (in the loop) if the remainder is zero (since then the number is not a prime) and if after the loop is finished (no false is returned) you want to return True, since the number is a prime. So you want to change the indent of return True, so that it returns True after the loop has finished


#5

I see.

Thank you. But now it fails at 4, it returns true instead of false

def is_prime (x):
    if x ==0:
        return False
    if x<2:
        return False 
    elif x == 2:
        return True 
    else:
        for n in range (2,x):
            division = float (x/n)
            if round(division,0)-division>0:
                return False
            else:
                return True

#6

return True is still reached in the first run of the loop, you should really change the indention of else return True, so that else is at the same indent level as the for loop, so it runs if the for loop doesn't return false


#7

Thank you.

I did it and the code still doesn't run for 4:

def is_prime (x):
    if x ==0:
        return False
    if x<2:
        return False 
    elif x == 2:
        return True 
    else:
        for n in range (2,x):
            division = float (x/n)
            if round(division,0)-division>0:
                return False
        else:
            return True

#9


#10

like i said already, you should use the modulo operator % to check if the remainder is zero. 4 / 2 gives two as a result. of course that doesn't work

use the modulo operator


#11

OK! it worked, still a bit confused on the indentation but ill figure it out. Thanks!


#12

well, if you have this:

for loop:
   if condition:
     return something
  else:
    return something else

in the first run of the loop, if is true or false. If if is true, something is returned. Otherwise if is false, else will run and something else is returned (return means: function ends, even if it is in the middle of a loop)

however if you have this:

for loop:
   if condition:
     return something
else:
    return something else

else will only run after the for loop is finished, if the if condition is true somewhere in the loop, a return keyword is reached, the function ends. So the only way else can run, is that the loop finish (if is false in all values for the loop)

You can even leave out the else keyword


#13

Oh I get it now! thank you !!!


#14