Python : TypeError: 'int' object is not iterable

Posted 缓下脚步

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了Python : TypeError: 'int' object is not iterable相关的知识,希望对你有一定的参考价值。

用循环依次对list中的每个名字打印出 Hello, xxx!

--------------------------------------------------------

L = [‘Bart‘, ‘Lisa‘, ‘Adam‘]
x = len(L)

for i in range(x):

print(‘Hello,‘, L[i])

--------------------------------------------------------

 

此处,若直接使用 for i in x 时,编译报错:TypeError: ‘int‘ object is not iterable:

Traceback (most recent call last):
File "main.py", line 5, in <module>
    for i in x:
TypeError: ‘int‘ object is not iterable

该问题的原因是:不能直接用int进行迭代,而必须使用range方法,即range(x).

 

 

以上是关于Python : TypeError: 'int' object is not iterable的主要内容,如果未能解决你的问题,请参考以下文章