Primes return false in is_prime task


#1



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

My code seems to be fine, but it says 1 returns true instead of false, AND no matter what number i put in except zero, i get false. I've seen other people's code, and know there is a built in function that makes it easy, but i want to avoid using it. However, my code doesn't work.

I was expecting a true for primes and a false for composites, but it hasn't worked out

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

I would love some help, thanks.
P.S, i stopped doing codecademy for a long time, so maybe i've forgotten something...


#2

Wait, the in bulit function was in a different one. Anyway, i just don't understand the solutions i've seen, so i need help


#3

i am going to give you a push in the right direction, please try again then, and if after a while you need more help, let me know. Okay, lets look at this line:

if x % y == True:

So, if the remainder of x divided by y is True, execute the if statement, make sense? Right, now you should know that positive numbers are considered True, where as zero is considered False. So currently, if there is a remainder, the if statement execute, don't you want to have the opposite?


#4

Thank you for the help. It helped a lot! Now, if only is_prime(1) returned False...


#5

Ok, I've finally got it. Thanks very much for the help, turns out that the "checking for remaiders" bit happened for 1 as well (x >= 1 INCLUDED 1) so is_prime(1) returned true, as the remainder checking cannot happen and it deduces it is true (the default else bit)