Pythonでのエラー:TypeError:'float' object cannot be interpreted as an integer
TypeError:'float' object cannot be interpreted as an integer
について。
”型のエラー:'float'オブジェクトは整数として受け取れません”
整数型しか受け取らない関数にそれ以外の型(小数、文字列etc)を渡すとこのエラーが返ってきます。
- 浮動小数:
'float'
- 文字列:
'str'
など。
解消法
整数を渡してください。
せっかくなので似たエラーを出してみる
range()に小数を渡す。
>>> range(0, 30, 0.1)
Traceback (most recent call last):
File "<stdin>", line 1, in <module>
TypeError: 'float' object cannot be interpreted as an integer
range()に文字列を渡す。
>>> range(0, 'hello', 1)
Traceback (most recent call last):
File "<stdin>", line 1, in <module>
TypeError: 'str' object cannot be interpreted as an integer
range()に宣言されていない変数を渡す。
>>> range(0, a, 1)
Traceback (most recent call last):
File "<stdin>", line 1, in <module>
NameError: name 'a' is not defined
listのインデックスに小数を渡してみる
>>> l = [1, 2, 3]
>>> l[0.1]
Traceback (most recent call last):
File "<stdin>", line 1, in <module>
TypeError: list indices must be integers or slices, not float
こちらも型違いが指摘されるが、文言が違った。
