Is_prime


#1



I am currently having trouble with the "is_prime" exercise.


I receive the error that this returns "True" for is_prime(9).

Since I have the program check whether x % n == 0 for n in range(2, x), I would expect that this program would see that 9 % 3 == 0 and thus would not return that 9 is prime. This indicates to me that there is some syntactical error I made somewhere but I am not sure where. Any help would be appreciated.


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


#2

a function ends the moment a return keyword is reached, which in your case happens in the first run of the loop.

You only want to return true after the loop has finished, so you might want to rethink your indention.


#3

I'm sorry but I don't understand exactly what you mean by "return keyword." Can you also elaborate/provide a link that explains what you mean by the function ends once the return keyword is reached?

Thank you


#4

in this sentence:

return False

you use the return keyword (keywords are purple in the exercise (def, print, if, elif) and so on. They have special meaning inside python

Return should have been covered, i can't find the exercise this quickly, i am sure it is somewhere.

well, the moment a function is running and it encounters a return keyword the function ends:

def example():
   return "let me show you"
   print "i will never be printed, a return keyword has already been reached so the function has already ended"
print example()

in your code, return is reached in the first run of the loop. you only want to return true if false isn't returned for any of the values in the for loop, to achieve this, you want to change some indention


#5

Okay so by moving the else statement to the left it separates the return from the second part of the loop?

Because after reading some other posts it seemed as if the issue was that in having that indent the code

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

would check for a single value of n and then move on to the else statement and return true. Why does interpreter read it in this manner?


#6

by moving else to the left, it would only run after the for loop has finished (which can only happen if false doesn't get returned)

Because that is how an if/else clause works? If if is true, execute if, else execute the code inside else

by moving else to the left, you get a for/else clause, if you can finish the for loop, execute else.


#7

Sorry I should have been more clear with my question. I would think that the interpreter would see the if statement as "check the divisibility for all numbers between 2 and x" and to only move to the else statement if none of those numbers divide x, yet it moves to the else statement after only checking 2 if the else is not indented. Why does that indentation make the difference between checking all the numbers in the specified range and only checking one?


#8

no, since the if and else are inside the for loop, one of both has to be executed. that is why you have to put the else outside the for loop (by changing the indent)

if there is a else, it will run the moment the if condition is evaluates to false.