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

 

 


免责声明!

本站转载的文章为个人学习借鉴使用,本站对版权不负任何法律责任。如果侵犯了您的隐私权益,请联系本站邮箱yoyou2525@163.com删除。



 
粤ICP备18138465号  © 2018-2025 CODEPRJ.COM