TypeError: 'int' object is not iterable -
In python 'int' object is not iterable. This error is shown whenever we are trying to iterate the integers.
Iterables - In python iterable is anything that can be looped over like list, string, files, etc. Integers, float, etc. are not iterable in python.
In the below example we are going to calculate the sum of numbers in a list.
The code for the program -
The output of the program -
Here in the add_list() function, we are using loop over the length of the list which is an integer, hence we are
getting TypeError. For calculating the sum of the list we need to iterate on the list.
The corrected code for the program -
The output of the program -
Try and Except is used for handling exceptions in python. The TypeError error is handled by it.
The code of the program using try and except -
The output of the program -
Comments