Is_prime() function


how come i get the error message:

"Your function fails on is_prime(5). It returns False when it should return True."

When I print is_prime(5) and it prints out TRUE? Plese help

remainder = []
def is_prime(x):
    if x == 0:
        return False
    if x == 1:
        return False
    for i in range(2, x):
        remainder.append(x % i)
    if 0 in remainder:
        return False
        return True
#print is_prime(5)


Try switching the 2nd ad 3rd if statements to elif statements to ensure the program is flowing correctly. Also, you may need to nest the for loop within a statement to run the tests correctly.


I think this code is more correct:

def is_prime(x):
    count = 0
    for i in range(1, x+1):
        if x % i != 0:
            count += 1
    if count == 2:


Thanks I fixed it now. :smiley:


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