Is_prime problem


#1

Emmm…please help me to check out the problem.why is it wrong?



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



#2

No else in the if statement (in the loop). Have the function return True at the very end, outside of the loop.


#4

What about this?

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

#5

Drop the else and move the return statement to the left so it lines up with the first if. It will not be part of any control flow structure. Just the last line of the function.


#6

Oh! OK I get it !!!
thxxxx!!!


#7

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