Python : TypeError: unsupported operand type(s) for +: 'NoneType' and 'int'

Asked 2 years ago, Updated 2 years ago, 105 views

You need to create a program that outputs the number of approximate numbers 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

def getCountsOfDivisors(n):
    count = 0

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

    print(count)

def isPrime(n):
    for f in range(2, n-1):
        if n % f == 0:
            print("False")
        else:
            print("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 code like this.

Traceback (most recent call last):
  File "C:\Users\Desktop\Desktop\202110943 Ahn Se-ho Practice Assignment #07.py", line 28, in <module>
    for t in range (2, num + 1):
TypeError: unsupported operand type(s) for +: 'NoneType' and 'int'

There's an error like this. What's the problem? It's the image I want.

python typeerror

2022-09-20 16:34

1 Answers

This is because there is no return value (return value) for the readNumber function.

Please add the return value as below.

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

First of all, the reason for the error is as above.

But I think other functions should have a return value.

I think you should find the part in the textbook that deals with the return value of the function and read it.


2022-09-20 16:34

If you have any answers or tips

Popular Tags
python x 4647
android x 1593
java x 1494
javascript x 1427
c x 927
c++ x 878
ruby-on-rails x 696
php x 692
python3 x 685
html x 656

© 2024 OneMinuteCode. All rights reserved.