Is_prime help needed!


#1

I thought this would work but for some reason, I keep getting a syntax error. Can someone see somethig i missed?

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


#2

for n in range(2, x):

is the correct syntax

Back off the indent on this so it lines up with for.


#3

Thanks! I got confused on it and it works now


#4

I followed the instruction step by step and why is it not (2, x-1) instead of ( 2, x) ??? what else is wrong in my code?? Thanks!

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

#5

That relates to what @mtf said. Align the else to the for (back off the indentation) and it should work.


#6

The range (2, x) will be [2, 3, 4, .., x-1]. If we use range(2, x-1) the range will be, [2, 3, 4, .., x-2], which makes less sense.

Technically, the range needn't be greater than [2, 3, .., x/2 ], or even [2, 3, .., sqrt(x)]. Give this some further study to see how we arrive at this conclusion.