In_Prime throws error messages


<Look to the right of this box. That is how your post will look to everyone else.>

<Below this line, add a link to the EXACT exercise that you are stuck at.>

<In what way does your code behave incorrectly? Include ALL error messages.>

<What do you expect to happen instead?>

<do not remove the three backticks below>

Replace this line with your code.
def is_prime(x):
    if x<2 :
        return False
    elif x == 2:
        return True
        for n in range(2,x):
            if x % n == 0:
                return False
                return True
x=int(raw_input('what is the number?'))
print is_prime(x) 

When I test if by input 7
I get following codeacademy error: our function fails on is_prime(9). It returns True when it should return False. Many thanks for your help.


The function exits once a return statement is reached. That means on the first iteration when n = 2, either True or False will be returned. Move the return True part out of the loop.


Many thanks. It was an indentation problem.


Can you paste the rectified code that worked for you ?


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