I am just stuck now. Cant figure it out.

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

is the closest i got

Replace this line with your code.



In the loop, if a condition is met, return something (true or false),
else is met (the condition) return something (true or false), etc.

Example -

if x < 2:
return False

Hope that helps.


you where so close. a function ends the moment a return keyword is reached, a function ends. In your case, in the first run of the loop either if is true and runs, or else will run, which means a return keyword is reached, the function ends (interrupting the loop)

You should have a for/else so that return True only occurs after the loop has finished running

Only if the for loop can finish running, the number is actually a prime


2 posts were split to a new topic: Is_prime


This topic was automatically closed 7 days after the last reply. New replies are no longer allowed.