Is_Prime(9) doesn't show the correct result


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:


A post was split to a new topic: Is prime differences in solution


Working code: This way you won't have to use elif or else anywhere in your code!!


What about the condition if x == 2?


@nskilly, please post your code with markup;

Otherwise it is really unreadable.

@aaron.stypa, if x == 2 is not needed, since if x is two both condition are false, so it will simply reach the return true on line 7.


Thank you for the advice :grinning:


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