6 is_prime code


#1

I've checked the other q&as for this but still can't work out my code keeps getting errors at is_prime(9)


#2

return false in the function


#3

Try this: replace last elif with else: and also change the indentation to be on the same level as for.
Remember to change return indentation as well.


#4

change the
for n in range(2, x):

to

for n in range(2, x-1):


#5

Sorry wrong person on that last answer.

This will work much better

`def is_prime (x):

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

#6

Thanks, that sorted it, not 100% sure why though


#7

It was just simplification and indentation errors you had


#8

Can any one please explain how this code

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

I am having doubt about how for loop works here