Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How do I fix TypeError: 'int' object is not iterable?

Tags:

python

I am trying to write a program that allows you to enter the number of students in a class, and then enter 3 test grades for each student to calculate averages. I am new to programing and I keep getting an error that I don't understand what it means or how to fix it. This is what I have so far:

students=int(input('Please enter the number of students in the class: '))

for number in students:
        first_grade=(input("Enter student's first grade: "))
        second_grade=(input("Enter student's second grade: "))
        third_grade=(input("Enter student's third grade: "))
like image 527
tinydancer9454 Avatar asked Feb 18 '13 16:02

tinydancer9454


People also ask

How do I fix TypeError type object is not iterable?

The Python "TypeError: 'type' object is not iterable" occurs when we try to iterate over a class that is not iterable, e.g. forget to call the range() function. To solve the error, make the class iterable by implementing the __iter__() method.

How do I make an int iterable in Python?

Iterators and Iterator Protocol So, in a gist, __iter__ is something that makes any python object iterable; hence to make integers iterable we need to have __iter__ function set for integers.

How do I fix TypeError float object is not iterable?

The Python "TypeError: 'float' object is not iterable" occurs when we try to iterate over a float or pass a float to a built-in function like, list() or tuple() . To solve the error, use the range() built-in function to iterate over a range, e.g. for i in range(int(3.0)): .

What is not iterable error?

The JavaScript exception "is not iterable" occurs when the value which is given as the right-hand side of for...of , as argument of a function such as Promise. all or TypedArray. from , or as the right-hand side of an array destructuring assignment, is not an iterable object.


2 Answers

Numbers can't be iterated over. What you're probably looking for is the range function, which will create a sequence of numbers up to the number you want:

for number in range(1, students + 1):

The reason I added + 1 there is because the second argument to range is exclusive.

like image 38
eagleflo Avatar answered Sep 30 '22 12:09

eagleflo


When you wrote

for number in students:

your intention was, “run this block of code students times, where students is the value I just entered.” But in Python, the thing you pass to a for statement needs to be some kind of iterable object. In this case, what you want is just a range statement. This will generate a list of numbers, and iterating through these will allow your for loop to execute the right number of times:

for number in range(students):
    # do stuff

Under the hood, the range just generates a list of sequential numbers:

>>> range(5)
[0, 1, 2, 3, 4]

In your case, it doesn't really matter what the numbers are; the following two for statements would do the same thing:

for number in range(5):

for number in [1, 3, 97, 4, -32768]:

But using the range version is considered more idiomatic and is more convenient if you need to alter some kind of list in your loop (which is probably what you're going to need to do later).

like image 67
bdesham Avatar answered Sep 30 '22 12:09

bdesham