Is_prime


#1


https://discuss.codecademy.com/t/6-is-prime-why-is-my-code-wrong/56403/10

it does not work for odd numbers


I expect it to work properly


Replace this line with your code.
 
def is_prime(n):
    if n==0 or n==1:
        return(False)
    elif n==2:
        return(True)
    else:    
        for x in range(2,n):
            if n%x!=0:
                return(True)
            else:
                return(False)


#2

a function ends the moment a return keyword is reached, in your case this happens in the first run of the loop. You should check if the remainder equals zero, if it does return false

then place return True outside the for loop so the whole can run (only if the whole loop can run, the number actually is a prime)


#3

return is not a function. When we wrap the value in parens we return a tuple. That may change things at the calling end.


#5

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