Python: The problem that the decimal function value appears as None when integers are 2 or 3 in the output

Asked 2 years ago, Updated 2 years ago, 23 views

We need to create a program that outputs the number of abbreviations and prime numbers of integers from 2 to n by receiving more than 2 integers, but it gets blocked at the output part.

def readNumber():
    while True:
        n = int (input ("Enter an integer")
        if (n >= 2):
            break
        else:
            print("Please enter a number greater than or equal to 2.")
            continue
    return n

def getCountsOfDivisors(n):
    count = 0

    for i in range(1, n+1):
        if n % i == 0:
            count += 1

    return count

def isPrime(n):
    for f in range(2, n-1):
        if n % f == 0:
            return False
        else:
            return True


num = readNumber()
for t in range (2, num + 1):
    print("integer:" + str(t) +"minimal number:" + str(getCountsOfDivisors(t)) +"minority:" + str(isPrime(t))))

I made the chords like this In the output, the decimal function value appears as None when integers are 2 or 3.

May I know what the problem is?

python

2022-09-20 16:34

1 Answers

In range (2, n-1), n-1 becomes a lower number than 0 if n is 2 or 3.

To activate the for statement, n-1 should be greater than 2, at least 3, but it doesn't.


2022-09-20 16:34

If you have any answers or tips


© 2024 OneMinuteCode. All rights reserved.