Prime numbers


#1

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

Oops, try again. Your function fails on is_prime(0). It returns None when it should return False.

It’s supposed to figure out if it’s prime or not, but I can’t see why it’s not working. Am I getting the logic wrong or syntax or is it something else entirely?



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



#2

for starters, for x=0, the loop never runs so no return keyword is ever reached, so None is returned (the default a function returns)


#3

Ohhh so the range doesn’t have a value since it’s between 2 and -1. Ok let me fix that. Thank you!


#4

the loop only runs when the stop value (x-1) is more then the start value (2)


#5

So this should work right?

def is_prime(x):
    if x < 2:
        return True

Cause 0 is less than 2 it should return true and then stop right? Only it doesn’t. it just returns the same error message.


#6

it does

no? It should be for a different number now. Please show code and error message


#7
def is_prime(x):
    if x < 2:
        return True
    elif x == 2:
        return False 
    else:
        for n in range(2, x-1):
            if x % n == 0:
                return False
            else:
                return True

the error is exactly the same:

Oops, try again. Your function fails on is_prime(0). It returns True when it should return False.


#8

whoah wait
nevermind I just noticed that


#9

values less then two can’t be prime numbers, so that should be return False. I should have noticed that as well


#10

Hmm. I changed the code a bit to get it to work but now it doesn’t work for 9 or 15 returning True instead of False. (all numbers that are divisible by three)

def is_prime(x):
    if x < 2:
        return False
    elif x == 2:
        return True
    elif x == 3:
        return True
    else:
        for n in range(2, x-1):
            if x % n == 0:
                return False
            else:
                if 
                return True

#11

i just explained the exact same problem here a while ago:

Please read it.


#12

So continue causes the loop to keep going? If so I think I get it.


#13

no, the problem is that return ends the function. But for a number to be a prime number, x musn’t be divisble by n, so we only want to return True after the loop, not in the first iteration of the loop.


#14

Ok and the if statement makes the loop continue to iterate through each of the items and only returns true if x isn’t divisible by any of n

def is_prime(x):
    if x < 2:
        return False
    elif x == 2:
        return True
    elif x == 3:
        return True
    else:
        for n in range(2, x-1):
            if x % n == 0:
                return False
            else:
                if n != (x-2):
                    continue
                else:
                    return True

#15

i don’t understand why you simply place return True outside of the loop, its so much easier. I guess linking to that topic was a bad idea.


#16

:man_facepalming: I get what you where trying to say. Sorry.

def is_prime(x):
    if x < 2:
        return False
    elif x == 2:
        return True
    elif x == 3:
        return True
    else:
        for n in range(2, x-1):
            if x % n == 0:
                return False
    return True

#17

That should work. Now you can shorten your code by getting rid of all the elifs and else. You don’t need them any more.


#18

Alright thanks! That explains things :slight_smile:

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

#19

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