初級 › TypeError: '>' not supported between instances の原因と直し方 › パターン3
TypeError: '>' not supported between instances の原因と直し方
文字列と数値、あるいはNoneと数値など、比較できない型同士を比較しようとしたときに出るエラーです。
1 x = ____ ^ 2 if x > 0: 3 print("positive")
Traceback (most recent call last):
File "main.py", line 2, in <module>
if x > 0:
^^^^^
TypeError: '>' not supported between instances of 'NoneType' and 'int'
空欄を埋めて実行を通す
初期値としてNoneを使った変数を、数値と直接比較するとエラーになります。値が入っているかを先にNoneチェックします。
次の問題