TypeError: 'int' object is not iterable
The error message “TypeError: ‘int’ object is not iterable” occurs when you try to iterate over an integer.
In Python, iteration is typically done with containers like lists, tuples, or sets. An integer, being a whole number without any elements inside, does not have elements to iterate over.
To fix this error, ensure that you’re iterating over a container like a list or tuple:
# Correct example
my_list = [1, 2, 3]
for item in my_list:
print(item)
In the code above, my_list
is a list that can be iterated over. This ensures that you won’t encounter the “TypeError: ‘int’ object is not iterable” error.
还没有评论,来说两句吧...