TypeError:“ int”类型的参数不可迭代
问题内容:
运行程序时出现此错误,我不知道为什么。该错误发生在显示“ if 1 not in c:”的行上
码:
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
我想知道的是,如何从仍然正常运行程序的情况下解决此错误。
提前致谢!
问题答案:
这c
是索引而不是您要搜索的列表。由于您无法遍历整数,因此会出现该错误。
>>> myList = ['a','b','c','d']
>>> for c,element in enumerate(myList):
... print c,element
...
0 a
1 b
2 c
3 d
您正在尝试检查是否1
在中c
,这没有任何意义。