6. is_prime help!



3 is returning "none" instead of false 9 and 15 are returning "True" instead of "False".
Why is this happening?

def is_prime(x):
    r = range(2, x-1)
    if x==0 or x==1:
        return False
    elif x==2:
        return True
    for i in r:
        if x%i==0:
            return False
            return True
num = int(raw_input("Enter a number to check if it is prime:"))
print is_prime(num)


Too much indentation. Back it off to match with the for.

Alternately, since there is no break in for statement, the else: can be dropped and just write return False, but also in line with the for, outside of the loop.


A post was split to a new topic: Returns 9 as a prime