Is_prime


#1



https://www.codecademy.com/en/courses/python-intermediate-en-rCQKw/0/6?curriculum_id=4f89dab3d788890003000096#


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
        else:
            return True


#2

What does return do?


#3

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


#4

its still wrong


#6

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?


#7

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


#8

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...


#9

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


#10

The code might not reach return if its off.

heres a simple example:

a = 30

def double(x):
return x * 2

double(a)

would return 60


#11

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