Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

TypeError: argument of type 'int' is not iterable

Tags:

python

I am getting this error when I run my program and I have no idea why. The error is occurring on the line that says "if 1 not in c:"

Code:

matrix = [
    [0, 0, 0, 5, 0, 0, 0, 0, 6],
    [8, 0, 0, 0, 4, 7, 5, 0, 3],
    [0, 5, 0, 0, 0, 3, 0, 0, 0],
    [0, 7, 0, 8, 0, 0, 0, 0, 9],
    [0, 0, 0, 0, 1, 0, 0, 0, 0],
    [9, 0, 0, 0, 0, 4, 0, 2, 0],
    [0, 0, 0, 9, 0, 0, 0, 1, 0],
    [7, 0, 8, 3, 2, 0, 0, 0, 5],
    [3, 0, 0, 0, 0, 8, 0, 0, 0],
    ]
a = 1
while a:
     try:
        for c, row in enumerate(matrix):
            if 0 in row:
                print("Found 0 on row,", c, "index", row.index(0))
                if 1 not in c:
                    print ("t")
    except ValueError:
         break

What I would like to know is how I can fix this error from happening an still have the program run correctly.

Thanks in advance!

like image 401
chingchong Avatar asked Dec 30 '11 01:12

chingchong


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.

Is not iterable JavaScript 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.

Which of the Python object is not iterable?

The Python TypeError: NoneType Object Is Not Iterable is an exception that occurs when trying to iterate over a None value. Since in Python, only objects with a value can be iterated over, iterating over a None object raises the TypeError: NoneType Object Is Not Iterable exception.

How do you iterate through an int object in Python?

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.


1 Answers

Here c is the index not the list that you are searching. Since you cannot iterate through an integer, you are getting that error.

>>> myList = ['a','b','c','d']
>>> for c,element in enumerate(myList):
...     print c,element
... 
0 a
1 b
2 c
3 d

You are attempting to check if 1 is in c, which does not make sense.

like image 69
Lelouch Lamperouge Avatar answered Sep 18 '22 15:09

Lelouch Lamperouge