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).

 

原文地址:https://www.cnblogs.com/Johnny-z6951/p/13336719.html