![](/img/trans.png)
[英]'<' not supported between instances of 'int' and 'NoneType'
[英]Solution to '>' not supported between instances of 'NoneType' and 'int' error?
我已經參考了有關此錯誤的其他問題,但似乎無法為我的案例找到解決方案。
a = function(x)
b = function(y)
if a == None:
print("Invalid a")
if b == None
print("Invalid b")
if a > b:
print("Review")
Error: '>' not supported between instances of 'NoneType' and 'int'
根據我的研究,我了解錯誤的含義,但我不確定如何修復它。 任何幫助將不勝感激。
您可以使用elif
:
a = function(x)
b = function(y)
if a == None:
print("Invalid a")
elif b == None
print("Invalid b")
elif a > b:
print("Review")
否則,仍然有可能a
或b
為None
因為所有比較在您的代碼中都是相互獨立的。
作為if/else
語句的替代方案,我建議使用try/except
塊,如果通常不滿足except
條件,這應該會更有效一些。
此外,根據PEP 8 ,與 None 的比較應該使用is
或is not
,而不是==
和!=
。
a = 21
b = None
try:
if a > b:
print("Review")
except TypeError:
if a is None:
print("Invalid a")
elif b is None:
print("Invalid b")
else:
raise
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.