is_prime: What I don't understand?

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

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

Or 9%3=0, so it's a Fasle but my code say True...


your for loop will only runs once, in the first run of the for loop, either the if statement evaluates to true, or else will run. Either way, a return keyword is reached, which means the function (which includes the loop) will end.

You might want to put else outside your for loop, this way the for loop will run for all values, if the number is and return False when the number is not a prime (put return false in your for loop), otherwise, it will go through all values, and return true, which is correct since the number is a prime number

Is prime

Thanks to you, I understand that my else is on the for loop. I found an other mistake and I take x%i==0 in the for loop.
Now it's working.... That was hard^^


yes, i was hoping you would catch that you needed x%i==0 in the for loop, that is really important.

It is difficult, but these kind of exercises are really good for your coding skill :slight_smile:


why when you return True outside your for loop, the result don't return True in any case...?


did you add a print is_prime(11)? then you should see true being printed