6/15. is_prime()


#1

Hello,

when trying to create my function is_prime(x) it keeps telling me that I didn't create a function.. what is wrong here?

def is_prime(x):
    for n in range(2,x):
        if n % x = 0:
            print "not a prime"
            return False
            break
        else:
            return True

it returns an

Oops, try again. Did you create a function called is_prime? Your code threw a "global name 'is_prime' is not defined" error.

anyone got any insight into why it does this?


#2

        if x % n == 0:

break is unreachable after return. That statement may be removed.


#3

I'll check it out, thanks. But my question is really about me getting a is_prime is not defined error , which pretty much ends the assignment before I can even start debugging.


#4

wow, that did it! What was wrong then? how is not reaching break the reason for it not having is_prime defined while I just defined it?


#5

The else statement is not needed, here. Back off the indentation to four spaces:

    return True

The break is not an issue, per se, just not reachable.


#6

Your code has a syntax error.
Which means that it does not run.
Which means that your code does not create that function.
Hence Codecademy's error message.

The way you are meant to identify this is by spotting the error message in the console.


#7

2 posts were split to a new topic: Your function fails on is_prime(0)


#8