<p>for循环应该迭代索引而不是行(列表),或者只获取每列中的元素。从生成矩阵(<code>generation[row][col]</code>)访问时,使用的行和索引似乎应该是int值,在for循环中访问行列表</p>
<p>请尝试以下操作:</p>
<pre><code>def displaygen(generation, win):
for row in range(len(generation)):
for col in range(len(generation[row])):
position = Point(5,5)
if generation[row][col] == '0':
circ = Circle(position,3)
circ.setFill('green')
circ.draw(win)
elif generation[row][col] == '1':
circ = Circle(position,3)
circ.setFill('red')
elif generation[row][col] == '2':
circ = Circle(position,3)
circ.setFill('black')
</code></pre>
<p>或者直接访问矩阵中的元素</p>
<pre><code> def displaygen(generation, win):
for row in generation:
for col in row:
position = Point(5,5)
if col == '0':
circ = Circle(position,3)
circ.setFill('green')
circ.draw(win)
elif col == '1':
circ = Circle(position,3)
circ.setFill('red')
elif col == '2':
circ = Circle(position,3)
circ.setFill('black')
</code></pre>