# Number 5. How is this wrong?

#1

Ive read the other threads on this, but i dont get how i am wrong? Maybe im just missing something. Help would be appreciated.

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

#2

On the first iteration, you'll return either False, or True.
Is one iteration enough to determine the correct answer and return?
Is it even a loop if you return in the first iteration?

You can use print statements to follow along in what's happening and compare what it does to what you expect.

#3

To expand on what @ionatan is saying,

What you have going on is

A prime number is a whole number greater than 1, whose only two whole-number factors are 1 and itself. The first few prime numbers are 2, 3, 5, 7, 11, 13, 17, 19, 23, and 29.
SOURCE:Definition Source

Now how do we get a whole number in a program then, how do we transfer what we know to this?

Steps:

1. A number greater than 1
2. Is the number only divisible by it's self and one?

Ok, now that we have the rules lets compare them to your program.

``if x < 2: return False``

Looks good, if a number is less than 2 it's False.

Next,

``elif x == 2: return True``

Nice, we know 2 is a prime

Now,

``````else:
for n in range(2, x):
if x % n == 0:
return False
else:
return True``````

So if a number is divisible by anything between 2 and it's self -1 it is not a prime, looks good.

Ok, let's test some stuffz, let's add a line to your code to see what's happening!

``````# Right after this line add the print statement
for n in range(2, x):
print("%s" % (x % n))``````

After you run that you will see your issue. It does only one number and because you have it immediately escaping with the `else: return` it will always return a number is prime.

Now here is an example of some code I have written, while it is a little verbose it should tell you what needs to be done and why all in the code it's self. If you don't get why something was done ask.

``````def is_prime(get_number, display=True):

prime = True

_numbers_to_number = [number for number in range(2, get_number)]

for number in _numbers_to_number:
if get_number % number == 0:
prime = False
break

if prime:
if display:
print("%s is a prime" % prime)
return get_number
elif not prime:
if display:
print("%s is not a Prime" % get_number)``````