gpt4 book ai didi

Python - 类型错误 - 类型错误 : '<' not supported between instances of 'NoneType' and 'int'

转载 作者:太空狗 更新时间:2023-10-29 17:47:38 25 4
gpt4 key购买 nike

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

我在 Stack Overflow 中寻找答案,发现我应该使用 int(input(prompt)),但这正是我正在做的

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

最佳答案

这个问题也出现了when migrating to Python 3 .

在 Python 2 中,将整数与 None 进行比较将“有效”,这样 None 就被认为小于任何整数,甚至是负数:

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

在 Python 3 中,此类比较会引发 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'

关于Python - 类型错误 - 类型错误 : '<' not supported between instances of 'NoneType' and 'int' ,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/43708541/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com