6. is_prime help!


#1



https://www.codecademy.com/en/courses/python-intermediate-en-rCQKw/0/6?curriculum_id=4f89dab3d788890003000096

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


def is_prime(x):
    x=int(x)
    x=abs(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
        else: 
            return True
num = int(raw_input("Enter a number to check if it is prime:"))
print is_prime(num)


#2

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.


#4

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