6 is_prime code


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


return false in the function


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.


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


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


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
    return True`


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


It was just simplification and indentation errors you had


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
        return True

I am having doubt about how for loop works here