6.is_prime, Your function fails on is_prime(9)


heres my code:
def is_prime(x):
__if x<2:
____return False
__elif x ==2 :
____return True
__for n in range(2, x):
____if x % n ==0:
______return False
_____return True

print is_prime(9)


Your for loop only runs once now, go ahead, and add a print statement after your for loop:

for n in range(2, x):
//rest of your code

what is happening? When return is reached, the function ends. In your first run of the loop, either the if condition is true, or it will go to else. A return is reached either way, something is returned, and the function ends. How to solve this? Well, you could simple remove your else, put return True on the same line as your for loop, then it will loop through all values, if the number is not a prime, the if statement will be true at certain point, return false, the function (and the loop obviously) ends, problem solved :slightly_smiling:


for next time, use one of the two following options to make your code/indent is visible:

select your code and press ctrl + shift + c (or cmd + shift + c if you use a mac)

if this instructions are unclear, you can also insert 3 backticks before and after your code, like so:


the backtick is located above the tab key on your keyboard.


No need to use break, return will end the function if if evaluates to true.


just remove the indent before the last else in your code so that you can see the else statement alligned with the for statemen .
i short make it a for/else statement.