Python--索引器:列表索引超出范围(数独解算程序)
如果这个代码不是最容易遵循的,我很抱歉。所以我有一个9x9维的二维列表Python--索引器:列表索引超出范围(数独解算程序),python,list,indexing,sudoku,solver,Python,List,Indexing,Sudoku,Solver,如果这个代码不是最容易遵循的,我很抱歉。所以我有一个9x9维的二维列表board(一个数独板)。在solve函数中的一行中,我检查行是否可以轻松重现您的回溯: >>> row, column = 8, 8 >>> board[row][column].isPermanent = True >>> add1(board, row, column) Traceback (most recent call last): File "<st
board
(一个数独板)。在solve
函数中的一行中,我检查行是否可以轻松重现您的回溯:
>>> row, column = 8, 8
>>> board[row][column].isPermanent = True
>>> add1(board, row, column)
Traceback (most recent call last):
File "<stdin>", line 1, in <module>
File "<stdin>", line 3, in add1
IndexError: list index out of range
因此column==8
为真,您将row
增加到9,并在下一次迭代中点击board[9]
具有讽刺意味的是,你的板
包含一个有效的、已解决的数独,但你没有检测到这一点。你能发布完整的异常吗?未触及的错误消息应指向导致异常的行。在add1
中,对于row=8、column=8和board[row][column]。isPermanent
为True,则以row+=1
和continue
结束<代码>线路板[9]
不存在。
>>> row, column = 8, 8
>>> board[row][column].isPermanent = True
>>> add1(board, row, column)
Traceback (most recent call last):
File "<stdin>", line 1, in <module>
File "<stdin>", line 3, in add1
IndexError: list index out of range
def add1(board, row, column): # increments each cell
while True:
if board[row][column].isPermanent:
if column == 8:
row += 1
column = 0
else:
column += 1
continue