Is_prime


#1



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


It makes sense to me that this should work (obviously it doesn't or else I wouldn't be posting this) but I don't understand what needs to be changed. I keep getting error messages saying: "Oops, try again. Did you create a function called is_prime? Your code threw a "global name 'is_prime' is not defined" error." and
"File "python", line 2
for n(2, x - 1):
^
SyntaxError: invalid syntax" (the arrow being right under the colon)


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


#2
for n in range(...):

Also, the if statement should not have an else. We only wish to return on False. Let the return True fall to the last line of the program, outside of any statement.


#3

thank you! i made those changes but now it is saying "Your function fails on is_prime(0). It returns True when it should return False." I made changes to try to fix it but they didn't work either. this is the new code (with the for statement indented once, if indented twice, return indented three times, and return true aligned with the for statement):

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

#4

We need simple validation to rule out values less than 2.

if x < 2: return False
for n in range(2, x):
    if x % n == 0: return False

Niether of and x == 0 and or x == 0 are needed.


#5

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