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
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 .
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() .
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 .
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'
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With