# Is_prime

#1

So my error message said "Oops, try again. Your function fails on is_prime(9). It returns True when it should return False." but I don't see why it returned True when x is 9. Can someone help me?

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

#2

a function ends the moment a return keyword is reached

if a return keyword is reached in a loop, the function breaks. so in case of `x=9`, we have the following condition in the first iteration:

``if (9 % 2) == 0:``

which evaluate to false, else will run, true will return, the loop will break, the function ends

#3

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

In this case, 9%2 is not 0, so `else` will run, but why would this return `False`?

#4

because there is just a variable update? No return keyword, so the loop will keep running. I personally dislike this solution, your solution (when working) is better

#5

So... how should I adjust my code to make it work?

#6

you should place return True outside the loop.

This way, when a number is not a prime number, at some point in the loop false will be returned (causing loop to break and function to end), if the whole loop finishes running, true will be returned (which is good, since this is the condition for a number to be a prime number)

you can then also get get rid of your elif x equals two condition

#7

I see! Thank you so much!

#8