Why isn't this working (is_prime)


#1


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

I've actually gotten this exercise to work a few times, I just don't understand why some versions run and some don't. This returns "None" on 3. I think the problem is return True and return False don't work in the nested for loop, but I do not understand why.

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

#2

We won't need the else, but the return line should match the indent of the for. We want it to return True only if the loop completes executing.


#4

Check out my code

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

#6