Python

【Python】「TypeError: argument of type 'int' is not iterable」エラーの対処方法!

あきぞらです。

Pythonをいじっていて、こんなエラーに出くわしました。

TypeError: argument of type 'int' is not iterable

パッと原因が分からなかったので、調べてみることに。

原因

数字で「in」演算子を使っているのが原因でした。

> 1 in 1234567
Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
TypeError: argument of type 'int' is not iterable

in演算子は、int型では使えないので、Stringに変えてあげる必要があります。

> "1" in "1234567"
True

こちらでもOK。

> str(1) in str(1234567)
True

頻繁に出くわすので、注意しましょう。

-Python

Copyright© あきぞらてっく , 2025 All Rights Reserved Powered by AFFINGER5.