Is_prime(x)


#1



The following is my code for is_prime, but I do not know where my error is.

When is_prime(9), the output is True instead of False


def is_prime(x):
    n = range(2, x)
    if x == 1 or x == 0:
        return False
    elif x == 2:
        return True
    else:
        for a in n:
            if x % a != 0:
                return True
                break
            else:
                return False


#2

a function ends the moment a return keyword is reached. Which happens in the first run of the loop. Since either if evaluates to true or else will run, either way, a return keyword is reached, so the loop can't run all possible values

I would reverse it, if the remainder is zero return false, else return True, but put else at the same indent level as the for loop the whole loop can run, or remove the else keyword altogether and put return True at the same indent level as the for loop.