Is_prime help


#1

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

Oops, try again. Your function fails on is_prime(9). It returns True when it should return False.
Back to Editor
6. is_prime


#2

A function ends the moment a return keyword is reached, if a return keyword is reached in a loop simply breaks. (so you don't need the break keyword)

anyway, a number (in your case stored in variable x) is only a prime number if its not divisible by any number in range 2 (included) and x (excluded), thus you only want to return true after the for loop has finished running


#3

You're awesome! ...............


#4