 # FAQ: Learn Python - Practice Makes Perfect - factorial FAQ: Learn Python - Practice Makes Perfect - is_prime

Sorry, the J = 0 was a line that I used from another idea to try to get the 9 as non-prime.

But thanks for pointing out the else, it was really useless and made me note another thing which allowed me to realize the error of the code

(I am not gonna tell the error as I feel I shouldn’t spoil the answer).

btw if a branch returns then the code that comes after the if-statement is already exclusive and doesn’t need to be inside else and the additional indentation that comes with it

``````if something
exit like so
otherwise do something else
``````

another thought is that the iteration you’re doing is “any” - if any of these numbers divide this number
several loop concepts have function equivalents, and any is one of them

And yeah I’m already using some silly fancy things.

``````return not any(x % d == 0 for d in range(2, x))
``````

could also…

``````divisors = range(2, x)
dividesX = lambda d: x % d == 0
return not any(map(dividesX, divisors))
``````

… I’m probably just making things complicated.
but at the same time, simpler, because I’m no longer writing the loop.

Figure out all the potential divisors (range)
Define a test for a single divisor (given a number d, does d divide x?)
Apply the test to each divisor (map)
Check if any passed (any)

1 Like

True. I believe this was one of the many mistakes I see people doing in this exercise (And I committed it myself as well), as you tend to think that you need to specify the program to go over other characteristics, despite it not being needed. (In reality, it wouldn’t make a difference, but rather add a line. Though, the fewer lines the better so…)

Not exactly. I mean, your second theory would be probably equivalent to the for loop idea in terms of lines, maybe faster as it is done through functions. But the first one seems pretty simple an good-looking.

If you range up to x, you will get modulo equals 0 even for prime numbers

Not a bug, but kind of misleading instruction:

This suggests that x is bigger than 2 while during test run you check for x=0 and x=1…

no you will not. you would need to include x for that to happen.

no it suggests that n should not exceed that amount, it says nothing about x’s value

Apologies for range pin, someone suggested range (2, x-1) and I forgot that range returns numbers except the last one.
As for " `n` from 2 to `x - 1` ," with x=0 we have to count backwards (n=2, n=1, n=0, n=-1) and then forwards again Also definition says " A prime number (or prime ) is a natural number greater than 1…" so I don’t understand why my code was tested for zero and one?

0 and 1 are valid inputs for your function. Your function is supposed to tell primes and non-primes apart, therefore both primes and non-primes are valid input.
If only primes were used as input then you could write your function like this:

``````def is_prime(n):
return True
``````

if you start at 0 and count upwards stopping before -5, then you will end up with zero values. This does not mean that -5 is greater than 0.

why can not this program run ? Why is my second else in white ? 