Am I not looping it correctly? I have this but wont work

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


What does return do?


you are going to need a if condition to check for numbers smaller then 2, those are all not prime numbers, return False


its still wrong


a function ends the moment a return keyword is reached, you want only to return true after the loop is finished running, how to achieve this?


Im pretty new at this so not sure exactly how to describe it. When you create a function the whole idea is you get an action after the code is finished. In the code above were aiming to return True or False


yes, but what happens if the return keyword is reached? Something is returned, the function exist. In your case, this happens in the first run of the loop...


So you would return the result of the function? What if you don't have the result yet, and return anyway?


The code might not reach return if its off.

heres a simple example:

a = 30

def double(x):
return x * 2


would return 60


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