6. is_prime


#1

anything wrong with my code?

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

#2

this line:

def is_prime(x):

what is the thing after x? my computer won't even display it. it should be a closing parentheses ())

you only want to return True after your loop has finished running, change the indent of else.

uhm... if you want to check if something is equal use two equal signs, a single equal sign means assigns.


#3

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

THIS DOES NOT WORK WHAT IS WRONG?


#4

for the exact same reason:

currently you loop runs once, either if or else will run, which means a return keyword will be reached, the function (thus the loop) ends


#5

Hello there im still not very sure which else you are talking about...


#6

nevermind, i got it

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


#7

How is mine not working?
def is_prime(x):
if x<2:
return False
elif x==2:
return True
else:
for n in range(2,x):
if x%n==0:
return False
else:
return True


#8

for exactly the same reason? please read the replies in this topic, they solve your problem


#9

:wink:
well it's kind of like a careless mistake...


#10

Change the indentation of the last else


#11

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