如何解决Python中'NoneType'与'int'实例不支持'>'的TypeError
解决「TypeError: '>' not supported between instances of 'NoneType' and 'int'」错误
- 错误原因:你的代码中
TempBotY或者PrevbottomY[length]其中一个是None类型,没办法和整数进行大小比较。 - 排查方向:
- 确认
TempBotY在调用CheckBottomYCoVaring函数前,已经被正确赋值为整数,没有出现未初始化或赋值为None的情况。 - 检查
PrevbottomY列表,确保索引length对应的元素存在,并且是整数类型,不是None。
- 确认
- 优化后的代码(增加非空校验+简化逻辑):
def CheckBottomYCoVaring(length): # 先校验变量有效性,避免None类型参与运算 if TempBotY is None or length >= len(PrevbottomY) or PrevbottomY[length] is None: return False # 可根据实际需求调整默认返回值 # 用绝对值简化差值判断逻辑 return abs(TempBotY - PrevbottomY[length]) < 3
内容的提问来源于stack exchange,提问作者shiv
相关产品推荐
相关产品推荐

