6. Is Prime


#1



https://www.codecademy.com/fr/courses/python-intermediate-fr-FR-py0yl/0/6?curriculum_id=5370f030fed2a866a2000001#


When I enter my code and try to run it, it tells me that my function blocked on 0 because "object is not callable"

That's what they say on the French version (Oups, merci de réessayer. Votre fonction a échoué sur 0 comme entrée car votre fonction indique "'bool' object is not callable" error.)


I don't really know what didn't work and I need your help to find out what it is


def est_premier(x):
    if x< 2:
        return False
    else:
        for i in range(2,(x-1)):
            if x%i == 0:
                return False
            else:    
                return True


#2

SCT (submission correctness test) for this exercise is faulty and unfortunately, it would not be fixed. This is old course and it is no longer supported. The English version of this course will be renewed soon.

So if you really want to pass this exercise you can use this workaround -> https://discuss.codecademy.com/t/15-6-is-prime-error-bool-object-is-not-callable-error/51042/4?u=albionsrefuge, or you can just skip this one.


Now, back to your code. There are some problems. Let's say that we want to check if 9 is a prime.

9 < 2 is False, so we are in the else block. We enter the for loop...

9 % 2 == 0 is False, so we are in the else block and your function returns True. But 9 is not a prime, right?

To fix this, else block should be connected to the for loop, not to the if:

def est_premier(x):
    if x< 2:
        return False
    else:
        for i in range(2,(x-1)):
            if x%i == 0:
                return False
        else:    
            return True

#3

For me this one worked perfectly, even though it is a bit longer

def is_prime(n):
    x = n
    if x == 0 or x == 1 or x < 0:
        out = False
        print n
        print out
        return out
    else:
        if x == 2:
            out = True
            print n
            print out
            return out   
        
    y = x-1 
    while y > 1:
        if n % y == 0:
            out = False
            print n
            print out
            return out
        else:
            y -= 1
    else:
        out = True
        print n
        print out
        return out

#6

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


#7

Managed to get through with this code:

def is_prime(x):

def is_prime(x):

if x == 2:
    return True

elif x == 1 or x <= 0:
    return False

else:
    n = x-1
    while n > 1:

        if x % n == 0:
            return False    
        else:
            n -= 1 
    else:
        return True

#8

thanks a lot for the help.


#9

You're very welcome :slight_smile:


#10

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