Is_prime is not working


#1

Hi this is my code and it isn't working so i need someone to explain why it isn't working and how i can fix it and thanks.

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

#2

i know this lazy, but i wrote an extensive explanation on it yesterday:

in short, for x=9, the first number tested by your loop is 2. so 9 % 2 == 0 is false, so else runs, False is returned, your function ends.

For a number to be a prime number, the whole loop needs to finish running, not the first number tested by loop


#3

Thanks for the reply I have edited my code but it is not working and i wish you can explain what you said a liitle bit

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

#4

did you click the answer i posted above? It contains a detail explanation

a function ends the moment a return keyword is reached

if a return keyword is reached in a loop, the loop breaks so the function can end

so for x=9 (9 is not a prime number), the first number tested by the loop is 2. 9 % 2 == 0 is false. So else runs, return True causes your function to end. Is that what you want for x=9?


#5

it's not working and it shows this error

"Oops, try again. Your function fails on is_prime(3). It returns None when it should return True."

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


#6

in range, why x-1? range doesn't includes its stop value, so no reason to subtract

Please tell me you read some of the things i posted above? Its also the answer to your problem. If you don't understand, let me know what of it, you don't understand


#7

if x>2, is the number divisible by n?

  • Let's think about it, for example, No for n=2, n=3... But when it comes to n=5, Yes. Now you should also finish searching (break) for n, along with return False.

  • If x is not divisible for all n variables, return True. Be careful. Is it if/else statement or for/else statement?(Is there any if/else statement in here?)

if x<2, we expect False.

Else, you know it's True...


#8

@stetim94
I tried your sol but still i gives error
"Oops, try again. Your function fails on is_prime(9). It returns True when it should return False."

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


#9

seriously? I explained two times in this topic why it fails for x=9, plus a more extensive explanation i linked to:

And so far, you have given no acknowledge you read it. Did you read it? Please show me your thought process so i can fill in any gaps, i can keep writing general explanations in slightly different wording, but i don't think that gets us anywhere


#11

Hi souravjamwal77!

I know you are learning (like me) and I know how difficult it is to break away from the thought process of solving the problem in the way that you currently are, but I recommend coming at the problem in a different way. For example, you don't need to use a for loop with range() to solve this problem.

I just spent like 2 hours trying to solve the problem. It was frustrating sometimes but I managed to solve it with a while loop. The tip I can give that might save you some of my frustration is to check your indentations and make sure your returns are making sense in your code.

If you want some more tips or if you want to see my answer you can send me a reply!


#12

Thnx @lizardscruff and @stetim94 I placed else outside the loop with for and it worked, Thnx to all


#14

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