6. is_prime


#1



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

Hello all,
I've been stuck on this exercise for a long time. This is what I've written so far, and the error message reads, "Your function fails on is_prime(9). It returns True when it should return False."

I've tried troubleshooting and looking up help, but I genuinely don't understand how it returns 9 as true. Any help is appreciated. :slight_smile:


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


6. is_prime
#2

It's happening because the return statements end the 'for' loop. It wasn't noticeable before because every number up until that point was either divisible by 2 (4,6,8) allowing the first 'if' statement in the loop to catch them or they were prime (5, 7) meaning they were taken care of in the second 'if' statement. 9 is the first number that is not a prime and is not divisible by 2.

The program doesn't show it well but what happens with your code is as follows:

9 goes into is_prime(x) as x
It isn't == 2 or == 3 so it moves on
It is greater than 2 so it moves into the 'for' loop
The first number your range checks in the 'if' statement is 2
9 realizes it isn't divisible by 2 so it moves into the 'elif'
The 'elif' is still using 2 as it's 'n' so 9 is again checked against 2, still remains not divisible and so returns true

Once the program receives a return the loop breaks, without checking any more numbers against 9

It took me about an hour to figure this out myself.
My solution was as follows

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

Using this method, if a number 'x' is applied that is cleanly divisible by any other number 'n' that is not 1 or 'x', the program returns False and then breaks. Otherwise the loop continues to iterate through the numbers until it hits the top of the range, at which point it stops, and having not run the break command, instead returns True.

Hope this helps!


6. is_prime
#3

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

Looking for help on this one...
Thanks in advance.


#4

Sorry about the above one, I didn't know it will trim the spaces ...:slight_smile:


#5

Oops sorry, the spacing in my post was wrong. Pulling back your 'return True' a tab so that it's in line with your 'for' statement should fix the problem. I'll also edit my post. Thanks!


#6

Thanks alot.
your response was really prompt. :smiley:


#7

Thank you for the help!! Your changes worked :slight_smile:


#8

A post was split to a new topic: 6. is_prime


#9

I was stuck on this question for a long time...thank you for your help and the detailed explanation! :smiley:


#10

Your changes fixed my code, thank you so much! But I just have one question: why would the 'return True' need to be in line with the for statement, and why would there not be an else: preceding it... Thank you!


#11

because the moment a return keyword is reached, the function ends.

If if and else would have the same indent level, in the first run of the loop a return keyword is reached (either if is true, or else will run), while you only want to return true after the loop has run all values, not in the first run