伙计们,我接到了一项任务,我必须在网格中的一个单元格周围找到 1 的数量。示例图中的示例我应该得到 3,因为单元格周围有 3 个 1。所以我做了代码,我做对了,但是当我使用一个函数来做相同的代码时,它给了我错误,我需要你的帮助。
def count_neighbours(((1, 0, 0, 1, 0),(0, 1, 0, 0, 0),(0, 0, 1, 0, 1),(1, 0, 0, 0, 0),(0, 0, 1, 0, 0),), 1, 2):
grid=count_neighbours[0]
row=count_neighbours[1]
col=count_neighbours[2]
length=len(grid)
s=0
d=0
for i in range(row-1,row+2):
for j in range(col-1,col+2):
if i>=0 and j>=0:
if i<=length-1 and j<=length-1:
if grid[i][j]==1:
s+=1
if grid[row][col]==1:
d+=1
total = s-d
return total
#error goes like this
def count_neighbours(((1, 0, 0, 1, 0),(0, 1, 0, 0, 0),(0, 0, 1, 0, 1),(1, 0, 0, 0, 0),(0, 0, 1, 0, 0),), 1, 2):
^
SyntaxError: invalid syntax