So this code that is supposed to work decided not to on codecademy


The is_Prime one.

No error message, just a logic error. When I compile this code anywhere else nothing is wrong, but when I try to do so on the exercise it tells me that 5/2 = 2. (Again, when I run the exact same code somewhere else, anywhere that I have tried, all goes well.)

I expected it to tell me that 5/2 does not equal 2, and to return that 5 is a prime number like it does everywhere else.

#Print statements added for debugging purposes.
def is_prime(x):
    a = True
    if x > 3:
        for n in range(2, x-1):
            print("Loop Start")
            if round(x/n) == (x/n):
                print (x, n)
                print (round(x/n), (x/n))
                a = False
    elif x == 2 or x == 3:
        print ("Special Values")
        print ("What's left?")
    return a

print (is_prime(5))


Ask yourself, "When does the number 3 ever come up in the definition of primes?"

The brute force approach we are using here does not need very much logic Divisibility is the ticket. We kick out the value 2 and send it straight past Go, and any that are divisible are False. Quick rid of all even numbers..


There is definitely a logic error. The only values that return true are the two "special values" . It looks like the concept of remainder is what is the challenge here. A composite (non-prime) number (n) will at some point return a % = 0 when iterating through the range (2, n-1). Using the module operator is the best way of finding primes/ non-primes.


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