How to resolve TypeError: ‘>’ not supported between instances of ‘NoneType’ and ‘float’ in Python

'>' not supported between instances of 'NoneType' and float

TypeError: ‘>’ not supported between instances of ‘NoneType’ and ‘float’ in Python is the topic I want to introduce today. To fix this error, you need to return the specified value of the function, or you can use functions that have a check function like isinstance() or type().

What causes the TypeError: ‘>’ not supported between instances of ‘NoneType’ and ‘float’ error?

The error occurs when you compare a None value with a float value.

Example: 

firstVar = None
secondVar = 1.2

if firstVar > secondVar:
    print('The first variable is larger than the second.')

Output:

Traceback (most recent call last):
 File "./prog.py", line 4, in <module>
TypeError: '>' not supported between instances of 'NoneType' and 'float'

The problem with the example above is that you cannot use the comparison operator to compare a None value and a float value.

Example:

def NoneFunc():
	return None

secondVar = 1.2
if NoneFunc() > secondVar:
	print('The function contains a larger value than the variable.')

Output:

Traceback (most recent call last):
 File "./prog.py", line 5, in <module>
TypeError: '>' not supported between instances of 'NoneType' and 'float'

Even if you declare a function that returns no value, comparing the function with a float will cause an error.

Example:

def floatFunc():
	print(1.3)

secondVar = 1.2
if floatFunc() > secondVar:
	print('The function contains a larger value than the variable.')

Output:

Traceback (most recent call last):
 File "./prog.py", line 5, in <module>
TypeError: '>' not supported between instances of 'NoneType' and 'float'

How to solve the TypeError: ‘>’ not supported between instances of ‘NoneType’ and ‘float’ error?

Return a specific value for the function

The above two examples show that not returning a specific value or returning None will cause an error. To overcome that error, the function you declare should return an integer, a float, or a specific value.

Example:

  • The function I initialize returns a float value.
  • Use the comparison operator to compare the returned function value with the variable value.
def floatFunc():
	return 1.3

secondVar = 1.2
if floatFunc() > secondVar:
	print('The function contains a larger value than the variable.')

Output:

The function contains a larger value than the variable.

Check the None object

To check if an object is a None object, use the isinstance() function or the type() function.

Example:

  • Use the isinstance() function to print out the messages.
def floatFunc():
    return None

firstVar = floatFunc()
print(type(firstVar))
secondVar = 1.2

# Use the isinstance() function to check the function's type
if isinstance(firstVar, type(None)) is True:
    print(f'{firstVar}')
elif firstVar > secondVar:
    print('The first variable is larger than the second.')
else:
    print('The first variable is less than the second.')

Output:

<class 'NoneType'>
None

Similarly, you can perform the test using the type() function.

Example:

def floatFunc():
    return None

firstVar = floatFunc()
secondVar = 1.2
 
# Use the type() function to check the function's type.
if type(firstVar) is type(None):
    print(f'{firstVar}')
elif firstVar > secondVar:
    print('The first variable is larger than the second.')
else:
    print('The first variable is less than the second.')

Output:

None

Summary

The topic TypeError: ‘>’ not supported between instances of ‘NoneType’ and ‘float’ done. Remember not to compare None with any other value because it can cause errors. If you have any comments on the article, please leave a comment below.

Maybe you are interested:

Leave a Reply

Your email address will not be published. Required fields are marked *