int object is not iterableAs we take python language under consideration, we can say that the error mainly arises while performing code, not with proper rules. One of the rules is to iterate the int object throughout while the program runs. If we do so, it will raise the error, named as int object is not iterable. Here the term ‘iterable’ means to go through each string or word till the end by iterating over it one by one.
From the above discussion, we are familiar with the error int object is not iterable arises whenever we iterate any integer value, which is impossible. This error has come under TypeError; the type error usually arises whenever we call any function or use an incorrect data type operator. For example, If we perform an addition operation between a string and an integer object, we will get some error; this error is mainly called a type error because the data type of both objects does not match. Thus, we can easily iterate the list objects using for loop, but we can not do the same thing with a number. We can not separate each digit of the number using for loop, we can do so if we will store that particular number in a list. Let us understand with the help of some examples: Examples of TypeError: int object is not iterableExample 1: Here, in this problem, we are provided with the random number, and we will try to iterate it using a for a loop. Explanation: Here in this above example, we can easily find that we want to iterate a number, let say 10, by reading it one by one. But, we can do so because it is combined treated as a single-digit and unable to break as a one by one digit. To get the desired output, we can rectify the above program and use the range function in it. The output of the above program: Correction of above program: In this correction part, we have replaced the j by passing it into a single function named as range; after passing the number in the range function then the python programming language will easily get to recognize the working of the built-in function range in python and its main role to return a sequence of number begins from 0 itself by default and then incremented one by one until it reaches to the end value, that is passed in the arguments of this function. The output of the above program: Enter a random number to check the type error: 10 0 1 2 3 4 5 6 7 8 9 Example 2: Here in this problem we are provided with the random list of numbers or may be of some string values and we will try to iterate it using a for loop. Explanation: Here in this above example, if we observe, we can easily find that we want to iterate a number, let say 10, by reading it one by one. But we can do so because it is combined treated as a single-digit and unable to break as a one by one digit. To get the desired output, we can rectify the above program and use the range function in it. The output of the above program: Correction of above program: In this correction part, we have replaced the j by passing it into a single function named as range; after passing the number in the range function then the python programming language will easily get to recognize the working of the built-in function range in python and its main role to return a sequence of number begins from 0 itself by default and then incremented one by one until it reaches to the end value, that is passed in the arguments of this function. The output of the above program: Example 3: Here, in this problem, we are provided with the random number, and we will try to iterate it using a for a loop. Explanation: Here in this above example, if we observe, we can easily find that we want to iterate a number, let say 5, by reading its one by one, but we can do so because it is combined treated as a single-digit and unable to break as a one by one digit. To get the desired output, we can rectify the above program and use the range function in it. The output of the above program: Correction of above program: In this correction part, we have replaced the num by passing it into a single function named as range; after passing the number in the range function then the python programming language will easily get to recognize the working of the built-in function range in python and its main role to return a sequence of number begins from 0 itself by default and then incremented one by one until it reaches to the end value, that is passed in the arguments of this function. The output of the above program: Enter a random number to check the type error: 10 0 1 2 3 4 5 6 7 8 9 Example 4: Here, in this problem, we are provided with a random number, and we want to separate all the digits of that number. Explanation: Here in this above example, if we observe, we can easily find that we want to iterate a number, let say 110892, by reading its one by one, but we can do so because it is combined and treated as a single-digit and unable to break as a one by one digit. But we store all the separated digits in a list to easily access each digit of the number, but in the above example, a type error arises where the int object is not iterable. Now for getting the desired output, we can rectify the above program. The output of the above program: Correction of above program: In this correction part, we have replaced the num by passing it into a single function named as str; by using this function, we can easily break the particular number and, in combined form, store them in a list, the python programming language will easily get to recognize the working of the built-in function str in python. The output of the above program: |
原创文章,作者:ItWorker,如若转载,请注明出处:https://blog.ytso.com/263305.html