. Advertisement .
..3..
. Advertisement .
..4..
Hello everyone, I’m in a trouble with the error: ”int object is not iterable”. Here is the program I run:
pupils=int(input('Let me know the number of pupils in the class: '))
for number in pupils:
math_group=(input("Let me know student's Maths grade: "))
chemistry_group=(input("Let me know student's Chemistry grade: "))
philosophy_group=(input("Let me know student's Philosophy grade: "))
This is error I’m getting:
# Output
Let me know the number of pupils in the class: 5
Traceback (most recent call last):
File "c:\\Projects\\Tryouts\\listindexerror.py", line 3, in <module>
for number in pupils:
TypeError: 'int' object is not iterable
I have read a lot of documents but I don’t know where the mistake is and how to fix it. Does anyone have some guggestions for me? Please leave your answer below. Thanks a lot!
The cause:
This error happens due to in Python, integers can not repeated directly because they hold an unique integer value and don’t contain the
**‘__iter__‘ **
method.Solution:
This error can be solved by the way using the
range()
method in the for loop if you still want to iterate integers object. This method will create a list of sequential numbers. Let’s follow below result: