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

DO pass

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


I don't understand what's wrong!


a function ends the moment a return keyword is reached

so for x=9, the first number tested by the loop is 2:

if 9 % 2 == 0

which is false, so else runs. True is returned, the function ends (so does the loop)

but for a number to a prime number, the number must finish running and return True

Place return True outside the loop

If the number is then not a prime number, the if condition will evaluate to True at some point, False will be returned and the function ends. Which is good, which is what you want

6 is_prime

I needed to understand the logic behind the problem before understanding how to program it, so I will try to help you out the best I can. The question asks us to define whether or not a number is prime by running it through a set of conditions. Your first two conditions are good. On your third, when you run 9 through it, it stops after one iteration because you return false. Personally, I think a for loop is a bad way to go about this. So, delete everything after "else:"...

Try a while loop and define your variables. The "n" value is the number you divide x (or 9 in this case) by. Therefore, you should set n = n-1 (otherwise it will divide 9/9 and the if statement will return True immediately, saying that 9 is a prime number, when it is not). After defining n, write your while statement. You can do this a number of ways but when I wrote my statement, I had the iterations loop over while n > 1. Therefore, if x = 9, n = 8, and it will loop until n=1.

Once we have the while loop written, we can get to the conditions. You can keep the statement "if x % n == 0: return false". After that we need an else statement that ensures we iterate through every number 9 could be divided by. When you write this else statement, do not include a return statement with it, just update the n value. It should be n= n -1.

Finally, you need a return statement that will return a value after the loop finishes running.


A for loop is a fine way to solve this problem? You can simply use for/else, switching solution might be confusing for learner, they gotten pretty far with there logic (in fact, in this case the learner is very close), too make this many changes might be confusing. We will see what the learner picks


I think I understand what you are saying, but I don't know what put return True outside the Loop means. Should I put the whole Else: statement outside the loop or just Return True outside the loop. Please tell me how to do that. Thanks!


i didn't say on purpose, because you can do both? you can get rid of the else and simply place return True outside the loop or place else outside the loop so you have for/else, then else will run when loop condition becomes false

i wanted you to choice which one you understood and felt comfortable with


Thank you soo much it worked !!!!