用循环依次对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).
来源:oschina
链接:https://my.oschina.net/u/4326655/blog/4417729