Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Python - TypeError - TypeError: '<' not supported between instances of 'NoneType' and 'int'

Tags:

python

TypeError: '<' not supported between instances of 'NoneType' and 'int'

I have looked for an answer in Stack Overflow and found that I should be taking an int(input(prompt)), but that's what I am doing

def main():           while True:             vPopSize = validinput("Population Size: ")             if vPopSize < 4:                 print("Value too small, should be > 3")                 continue             else:                 break  def validinput(prompt):     while True:         try:             vPopSize = int(input(prompt))         except ValueError:             print("Invalid Entry - try again")             continue         else:             break 
like image 840
Dino3GL Avatar asked Apr 30 '17 16:04

Dino3GL


People also ask

How do I fix TypeError not supported between instances of STR and int?

The Python "TypeError: '<' not supported between instances of 'str' and 'int'" occurs when we use a comparison operator between values of type str and int . To solve the error, convert the string to an integer before comparing, e.g. int(my_str) < my_int .

How do I fix TypeError <UNK> not supported between instances of function and int?

Conclusion # The Python "TypeError: '>' not supported between instances of 'function' and 'int'" occurs when we use a comparison operator between a function and an integer. To solve the error, make sure to call the function with parentheses, e.g. my_func() .

What is class NoneType in Python?

NoneType in Python is a data type that simply shows that an object has no value/has a value of None . You can assign the value of None to a variable but there are also methods that return None .


1 Answers

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' 
like image 50
MartyMacGyver Avatar answered Sep 28 '22 10:09

MartyMacGyver