Python : TypeError: 'int' object is not iterable

时间:2020-07-18 22:45:27   收藏:0   阅读:95

用循环依次对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).

 

 

评论(0
© 2014 mamicode.com 版权所有 京ICP备13008772号-2  联系我们:gaon5@hotmail.com
迷上了代码!