close

[Solved] Python – TypeError – TypeError: ‘<' not supported between instances of 'NoneType' and 'int'

Hello Guys, How are you all? Hope You all Are Fine. Today I get the following error Python – TypeError – TypeError: ‘<‘ not supported between instances of ‘NoneType’ and ‘int’ in python. So Here I am Explain to you all the possible solutions here.

Without wasting your time, Let’s start This Article to Solve This Error.

How Python – TypeError – TypeError: ‘<‘ not supported between instances of ‘NoneType’ and ‘int’ Error Occurs?

Today I get the following error Python – TypeError – TypeError: ‘<‘ not supported between instances of ‘NoneType’ and ‘int’ in python.

How To Solve Python – TypeError – TypeError: ‘<‘ not supported between instances of ‘NoneType’ and ‘int’ Error ?

  1. How To Solve Python – TypeError – TypeError: '

    To Solve Python – TypeError – TypeError: '<' not supported between instances of 'NoneType' and 'int' Error you need to add a return in your function to get the number you input, otherwise it return an implicit None

  2. Python – TypeError – TypeError: '

    To Solve Python – TypeError – TypeError: '<' not supported between instances of 'NoneType' and 'int' Error you need to add a return in your function to get the number you input, otherwise it return an implicit None

Solution 1

This problem also comes up when migrating to Python 3.

In Python 2 comparing an integer to None will “work,” such that None is considered less than any integer, even negative ones:

>>> None > 1
False
>>> None < 1
True

In Python 3 such comparisons raise a TypeError:

>>> None > 1
Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
TypeError: '>' not supported between instances of 'NoneType' and 'int'

>>> None < 1
Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
TypeError: '<' not supported between instances of 'NoneType' and 'int'

Solution 2

you need to add a return in your function to get the number you input, otherwise it return an implicit None

def validinput(prompt):
    while True:
        try:
            return int(input(prompt)) 
            # there is no need to use another variable here, just return the conversion, 
            # if it fail it will try again because it is inside this infinite loop
        except ValueError:
            print("Invalid Entry - try again")


def main():      
    while True:
        vPopSize = validinput("Population Size: ")
        if vPopSize < 4:
            print("Value too small, should be > 3")
            continue
        else:
            break

or as noted in the comments, make validinput also check if it is an appropriate value

def validinput(prompt):
    while True:
        try:
            value = int(input(prompt)) 
            if value > 3:
                return value
            else:
                print("Value too small, should be > 3")
        except ValueError:
            print("Invalid Entry - try again")


def main():      
    vPopSize = validinput("Population Size: ")
    # do stuff with vPopSize

Summery

It’s all About this issue. Hope all solution helped you a lot. Comment below Your thoughts and your queries. Also, Comment below which solution worked for you? Thank You.

Also, Read