2011年12月2日 星期五

[python]int None type TypeError: int() argument must be a string or a number, not 'NoneType'


TypeError: int() argument must be a string or a number, not 'NoneType'

常常在做操作或有一個問題就是把string轉成int做比較,
可是卻因為某個沒有值的None type讓程式出錯了,
為了避免這個問題,
使用了這一個小技巧。

int(value or 0)
這樣就可以把其他值轉成0
減少程式發生錯誤了。

python how to convert Nonetype to int or string - Stack Overflow
http://stackoverflow.com/questions/3930188/python-how-to-convert-nonetype-to-int-or-string


沒有留言:

張貼留言