Is_prime() function


#1



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

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
    else:
        return True
        
#print is_prime(5)


#2

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.


#3

I think this code is more correct:

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

#4

Thanks I fixed it now. :smiley:


#5

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