Carl Wolter
Carl Wolter

Reputation: 23

Why my isPrime function returns True for 187?

I'm fairly new to coding so forgive me my lack of knowledge. I am trying to write a function which checks if a number is prime, yet for some non-prime numbers I test it returns as True. Can anyone explain why that may be so I can find a way to fix it?

def isPrime(num):
    if num > 1:
        for i in range (2,num):
            if num % i == 0:
                return False
            else:
                return True


isPrime(113)
Out[50]: True

isPrime(187)
Out[51]: True

Upvotes: 0

Views: 88

Answers (1)

ruohola
ruohola

Reputation: 24087

Your code almost works, it could for example be like this:

def isPrime(num):
    if num > 1:
        for i in range (2,num):
            if num % i == 0:
                return False
    return True

Upvotes: 2

Related Questions