Python TypeError: ‘int’ object is not iterable
Tags: python, programming, codenewbie
Published: 2021-09-05 10:32:11

---
title: Python TypeError: ‘int’ object is not iterable
published: true
date: 2021-09-05 06:25:18 UTC
tags: #python #programming #codenewbie
canonical_url: https://itsmycode.com/python-typeerror-int-object-is-not-iterable/
---

ItsMyCode |

If you have read our previous article, the  **‘** [**NoneType’ object is not iterable**](https://itsmycode.com/typeerror-nonetype-object-is-not-iterable/). You already know why Python throws ‘ **typeerror** ‘, and it occurs basically during the iterations like for and while loops.

## What exactly is TypeError: ‘int’ object is not iterable?

The most common scenario where developers get this error is when you try to iterate a number using for loop where you tend to forget to use the` range()` method, which creates a sequence of a number to iterate.

Consider the following code snippet to accept grades for each student in a class.

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

for number in students:
        math_grade=(input("Enter student's Maths grade: "))
        science_grade=(input("Enter student's Science grade: "))
        social_grade=(input("Enter student's Scoial grade: "))

# Output

Please enter the number of students in the class: 5
Traceback (most recent call last):
  File "c:\Projects\Tryouts\listindexerror.py", line 3, in <module>
    for number in students:
TypeError: 'int' object is not iterable
```

The above code is pretty straightforward, which reads input on the total number of students in a class, and for each student, it accepts the subject grades. 

The easier way everyone thinks here is to go with for loop and iterate the number of students to accept the grade. If you run the code, Python will throw a  **TypeError: ‘int’ object is not iterable**.

## Why does Python throw  **TypeError: ‘int’ object is not iterable?**

In Python, unlike lists, integers are not directly iterable as they hold a single integer value and do not contain the  **‘` __iter__ `‘ ** method; that’s why you get a **TypeError**.

You can run the below command to check whether an object is iterable or not. 

```python 
print(dir(int))
print(dir(list))
print(dir(dict))
```

[![TypeError: ‘int’ object is not iterable](https://cdn.itsmycode.com/wp-content/uploads/2021/09/image.png)](https://cdn.itsmycode.com/wp-content/uploads/2021/09/image.png)

[![python typeerror int object is not iterable	](https://cdn.itsmycode.com/wp-content/uploads/2021/09/image-1.png)](https://cdn.itsmycode.com/wp-content/uploads/2021/09/image-1.png)

If you look at the output screenshots, int does not have the ** ‘\_\_iter\_\_’ ** method, whereas the list and dict have the  **`'`\_\_iter\_\_`'` ** method.

## How to fix TypeError: ‘int’ object is not iterable?

There are two ways you can resolve the issue, and the first approach is instead of using int, try using list if it makes sense, and it can be iterated using for and while loop easily.

Second approach if you still want to iterate int object, then try using the `range() `method in the for loop, which will eventually generate a list of sequential numbers.

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

for number in range(students):
        math_grade=(input("Enter student's Maths grade: "))
        science_grade=(input("Enter student's Science grade: "))
        social_grade=(input("Enter student's Scoial grade: "))

```

The post [Python TypeError: ‘int’ object is not iterable](https://itsmycode.com/python-typeerror-int-object-is-not-iterable/) appeared first on [ItsMyCode](https://itsmycode.com).