Is_prime


#1



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


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

is the closest i got


Replace this line with your code.


#2

Hello

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.


#3

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


#4

2 posts were split to a new topic: Is_prime


#5

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