Is_prime


#1



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

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


I thought that this code would show me if x is divisible by any number between itself and 1, but for some reason it is coming back that prime numbers are divisible by themselves, not really sure were I went wrong.


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


#2

here:

for n in range(1, x):

you start value is one? why? this means 5 % 1 == 0 which is true. All numbers are divisible by 1, don't use it in the loop


#3

it does the same thing if I replace 1 with 2


#4

no? The error message changes. That is not same thing

so, now for x=5, your function returns None. So no return keyword is reached, why? And how do you think you can fix it?


#5

I honestly don't know. The way I see it should run 5 is greater than 3 so for every number between 2 and 5 if 5 is divisible by it return False and break loop. and if it runs through the loop it will return true, seeing as it doesn't meet the standards of all the other if statements.


#6

no, it will not return True because the elif condition is true. If the elif condition is true, other elif (you don't have this) and else condition won't run. else only runs when if and elif are false


#7

so how do I get it through the for loop?


#8

you get through the loop? after the loop, you need to return True


#9

I feel that you don't really want to help. every time I ask a question you send back a question. I'm understand that you have a greater knowledge of python than me, I was just hoping by posting my questions that I would get a little more insight on what I was doing wrong and an explanation, or some guidance on how to find a solution to this problem.


#10

and answer? lets walk through it for x=5

if 5 <= 1:

which is false, we move on to elif:

elif 5 > 3

which is true, then we get successfully through the loop (the if condition is never true)

then the function ends, given elif condition was true, else won't run. So, the function returns None (the default, given no return keyword was reached)

but we want to return True, given we determined 5 is a prime number

I want you to reach the conclusions successfully on your own, of course i can give you the answer, but you won't learn much from it. Questions can be guidance, why can't they be?


#11

Thank you this information is much more informative on what exactly I'm doing wrong. I don't want you to give me the answer but this is more of what I was looking for.


#12

I was able to figure out what I was doing wrong, up until this point I didn't realize that because the statement within the elif was true it stopped the number from running the for loop. thank you again for your help.


#13

what exactly does this mean? I think it means what i have been trying to tell:

given elif is true, else won't run so true is never returned for prime numbers


#14

I mean that because I wrote (elif x > 3:) and then wrote the for loop underneath it was finding that x > 3 is True and then didn't run it through the for loop. I solved the issue by not including the elif statement all together and wrote the for loop with an if statement inside of it so that it would return false after running the number through the loop. And yes I can clearly see that that's what you were telling me in this post.

But I see no mention of that before the above statement.


#15

sorry to be rude, but i don't you think fully understand, this is your code with print statement and function call:

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

print is_prime(5)

as you can see, the loop is running fine.

I am happy that you solved it, but it will take some more time to fully understand, look:

def is_prime(x):
    if x <= 1:
        return False
    elif x > 3:
        for n in range(2, x):
            print n
            if x % int(n) == 0:
                return False
    print "the else is about to be skipped"
    else:
        return True
    print "see? Else is skipped, otherwise the function would have ended, and this message wouldn't have been printed"
print is_prime(5)

#16

Alright I'm assuming I'm doing a poor job of reiterating what you have told me before. So let me rephrase. Yes I understand.
Also this sort of response is what I was talking about earlier it really doesn't seem that you are here to help any one, it really seems that you want to put people down and show off how much you know. If your motivation was to help you wouldn't have to say things like "sorry to be rude"


#17

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