Python TypeError: ‘int’ object is not iterable

Use a loop to print out each name in the list in turnHello, xxx!

——————————————————–

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

for i in range(x):

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

——————————————————–

Here, if you use for i in x directly, the compiler reports an error.TypeError: ‘int’ object is not iterableļ¼š

Traceback(mostrecentcalllast):
File”main.py”,line5,in<module>
foriinx:
TypeError:’int’objectisnotiterable

The reason for this problem is that you cannot iterate directly with int, but must use the range method, i.e. range(x).

Similar Posts: