TypeError: 'int' object is not iterable

原创 本是古典 何须时尚 2024-12-30 13:36 110阅读 0赞

The error message “TypeError: ‘int’ object is not iterable” occurs when you try to iterate over an integer.

In Python, iteration is typically done with containers like lists, tuples, or sets. An integer, being a whole number without any elements inside, does not have elements to iterate over.

To fix this error, ensure that you’re iterating over a container like a list or tuple:

  1. # Correct example
  2. my_list = [1, 2, 3]
  3. for item in my_list:
  4. print(item)

In the code above, my_list is a list that can be iterated over. This ensures that you won’t encounter the “TypeError: ‘int’ object is not iterable” error.

文章版权声明:注明蒲公英云原创文章,转载或复制请以超链接形式并注明出处。

发表评论

表情:
评论列表 (有 0 条评论,110人围观)

还没有评论,来说两句吧...

相关阅读