Prime help?


#1

Hi, I'm a bit disheartened by this course.

Why did this pass:

def is_prime(x):
if x < 2:
return False
elif x == 2:
return True
for n in range(2, x):

    if x % n == 0:
        return False
else:
    return True

But not this:

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

Or this: The instructions seemed to request a range input of 2, x-1 at point number 2:

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


#2

Use code formatting!

Your second and third code works with proper indentations:

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

But it's a really bad written code. elif inside the loop? It's better to check if x is equal to 2 at the beginning of the function.