Returns 9 as a prime


#1

my code is giving me issues saying it returns 9 as a prime I have been looking at this for three days help much appreciated:

def is_prime(x):
    if x <2:
        return False
    elif x == 2 or x == 3:
        return True
    else:
        a = range(2,x)
        print a
        for n in a:
            if x % n == 0:
                return False
            else:
                return True

print is_prime(9)

6. is_prime help!
#2

Have you tried executing it manually with the argument 9? What do you end up returning? Where should it have gone differently?
If you end up at 9 not being prime, then you must have misunderstood how something executes. Use print statements to find out what really happens.


#3

Ask yourself how many loops you will do.


#4

you were right it only do it once it test the first number from the sequence, this is my solution I thought for it

    def is_prime(x):
        if x < 2:
            return False
        elif x == 2 or x == 3:
            return True
        else:
            a = range(2,x)
            for n in a:
                if x % n == 0:
                    return False
            for i in a:
                if x % i != 0:
                    return True

    print is_prime(9)

#5

Yes, that is better! :+1:
Now think about when this becomes true and how you can write it shorter. :slight_smile: