samedi 13 juin 2020

Why does my code think 25 is a prime number?

I have written the following code to produce a list of divisors of a number, if a number is prime then it will return a string instead.

When I run the following code, it returns "25 is prime" instead of [5]. Where am I going wrong here?

def divisors(integer):
    divs = []
    for i in range(2, integer):
        if integer % i == 0:
            divs.append(i)
        if len(divs) == 0:
            return (F"{integer} is prime")
    return divs

print(divisors(25))

Aucun commentaire:

Enregistrer un commentaire