Python – How to solve ‘int’ object is not iterable
In this post we will give you information about Python – How to solve ‘int’ object is not iterable. Hear we will give you detail about Python – How to solve ‘int’ object is not iterableAnd how to use it also give you demo for it if it is necessary.
we are going to learn about how to solve the TypeError: ‘int’ object is not iterable in Python.
The int object is not iterable error occurs, when we try to iterate over a integer value using for loop, python complier throws the error.
Here is an example:
price = 434for i in price: print (i)
Output:
Traceback (most recent call last): File "main.py", line 11, in <module> for i in price:TypeError: 'int' object is not iterable
In the example above, we are trying to iterate over a integer, but integer objects are not iterable in Python.
To solve the error, convert the integer value to a string using str() function then iterate over it, because strings are iterable in Python.
Here is an example:
price = 434result = str(price)for i in result: print (i)
Output:
434
Note: If you want convert the string to a integer back, you can use the int() function in Python.
We can also run the for loop number of times by passing the integer value to a range() function.
counter = 6for i in range(counter): print(i)
Output:
012345
The range() function starts from 0 and ends before the passed number.
Conclusion
The “int” object is not a iterable error occurs, when we try to iterate over the integer value as a list or string, etc. To solve the error, convert the integer to string using str() function then iterate over it.
Hope this code and post will helped you for implement Python – How to solve ‘int’ object is not iterable. if you need any help or any feedback give it in comment section or you have good idea about this post you can give it comment section. Your comment will help us for help you more and improve us. we will give you this type of more interesting post in featured also so, For more interesting post and code Keep reading our blogs