Is_prime - feedback on my different approach


Hi all!

I'm quite new and I came up with a slightly different approach to this problem. Essentially it creates a list of divisors, checks to see if that list contains 2 items(1 and x) and if so returns True, else returns False.

I was hoping to get some feedback from the more experienced here. The solution worked but I'm not sure if this was a good way to solve.


def is_prime(x):
    if x < 2:
        return False
    elif x == 2:
        return True
        num = x
        lis = range(1,num +1)
        divi = []
        for i in lis:
            if num % i == 0:
        if len(divi) == 2:
            return True
            return False


This is a great solution to the problem. While it may not be the quickest solution to the problem, it is most certainly thinking "outside the box!"


Thanks for that.

I now realise for starters that I needlessly created the num variable when I could have just use x.


This topic was automatically closed 7 days after the last reply. New replies are no longer allowed.