Is_Prime(9) doesn't show the correct result


Oops, try again. Your function fails on is_prime(9). It returns True when it should return False.

For some reason, is_prime(9) returns true which it shouldn't obviously since 9 is divisible by 3. 3 is included though in the range (2, x)...
Can someone help me to detect the error?

Replace this line with your code. 
def is_prime(x):
    if x == 2:
        return True
    elif x >= 2:
        for n in range(2, x):            
            if x % n == 0:
                return False
                return True
        return False


Did you check other topics on this forum? I explained this problem several times in the last 7 days. And i am sure other people did as well


Wow thanks for the help! No need to reply on this one :wink:


Did you find a topic which explained your problem and you could solve it based on this explanation?


No I didn't, I checked many out and usually I have a similar code but for some reason it doesn't work with mine,but I'll keep looking...


there are less then i thought, but here is one.

It explains your problem in detail


I read everything but I didn't get it...I've got the exact same code that's supposed to work but I still get the same error message


Okay, if you read the whole thing, you must realize that your loop will only do one iteration? Because a return keyword will be reached in the end of the first iteration (either return True or return False), the function will end causing the loop to break

But you only want to return True after the loop has finished running


But the if-else is supposed to correct that? For example if x = 3, it will return true and then stop, if x = 9 then the first "if" will check if it is divisible by any number between 2 and 8, but for some reason it doesn't seem to divide 9 by three so it shows false... How can I correct that?


what you say here:

is wrong. This is not what will happen. What will for x = 9, is that the first number that will be checked is 2, so your if condition:

9 % 2 == 0

this condition will evaluate to false, because you have an if/else clause else will execute (because if was false), else will return True, when a return keyword is reached, the function ends. The loop simply breaks


Okay now I'm definitely lost, what does the condition have to be then to go trough all the numbers from 2 to x-1 without breaking the loop??


Else has to go outside the loop. So you get for/else instead of if/else

This way, when the number is not a prime number, the if condition will be true at some point in the loop, return False and break the loop. Everything cool

If the number is a prime number, the if condition will never evaluate to true, the loop finishes running, else will return True, you now successfully determined a number is a prime number.


Ooooh okay now I see more clear... I thought that an if will always have to be followed by at least an else.
Thank you for your time and sorry for bothering


no, why would that be? You should only include else if you want to do something else when if (optional, elif cases) are not met. if can be perfectly used without else


Try this like this :

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

i was on the same exercise , i took your code and changet it a bit , with help of Python visualize


I agree that python visualize would help, posting answers like you did is not according to the guidelines


But he did everything right , the only problem was ... let's say an "IndentationError" .
Also many times the instructions are not so "obvious" or clear


Yes, and he fixed it because i explained him. Which gives him more insight then when he looks at solution and thinks: that was it, and moves on.

Of course they are not in the practice makes perfect track, that is the point. There is the difference between coding (following a set of instructions) and programming (designing a solution)


Probably you are right , i just wanted to help and didn't see a problem to show him his mistake ...
you know , right a code and talk about it with one of your friends ?! So i thought he will stop do this mistake if someone will show where he is doing most mistakes ....

Also this might lead him to that, he need to repeat loops and "if" statements



isn't there a difference between showing a mistake and a full correct solution?

For the rest i agree with you :slight_smile: