Python for循环范围函数导致无限循环

2024-05-12 19:11:43 发布

您现在位置:Python中文网/ 问答频道 /正文

我正在构建一个数独函数来学习如何用python编写代码。我似乎在用for循环创建一个无限循环,但我不明白怎么做。代码尝试查看数独板的每个空方块,并检查数独规则是否允许值counter。如果允许计数器,则更新电路板,并将函数移到下一个空方块。如果不允许使用计数器,则计数器将递增1,然后再次测试。你知道吗

我遇到的问题是计数器大于9时。当这种情况发生时,我想看看之前的方块是空的,在原来的董事会(命名为拼图)和删除这个方块中的值。函数than应该将计数器设置为上一个平方+1中的值,并调用自身再次运行。你知道吗

本质上,函数是在测试每个空方块中的可能值,直到它找到一个值,然后移到下一个方块。如果没有可能的值,函数将返回跟踪,删除最后一个正方形,然后再次尝试运行。你知道吗

当计数器大于9时,我的问题似乎发生在else条件下。函数的这一部分导致了一个无限循环,它反复打印出“no”。你知道吗

我假设我的函数在while循环中卡住了,但我不知道为什么。你知道吗

puzzleBoard =[[1,2,3,4,5,6,7,8,9],[0,0,0,0,0,0,0,0,0],[0,0,0,0,0,0,0,0,0],[0,0,0,0,0,0,0,0,0],[0,0,0,0,0,0,0,0,0],[0,0,0,0,0,0,0,0,0],[0,0,0,0,0,0,0,0,0],[0,0,0,0,0,0,0,0,0],[0,0,0,0,0,0,0,0,0]]


def solvePuzzle():

#start by looking at each square in the 9x9 sudoku grid and check if that square is empty (=0)
for i in range(9):
    for j in range(9):
        counter = 1
        topX = 3*(i//3)
        topY = 3*(j//3)

        # while the board at index [i][j] is empty check if the value of 'counter' fits in the square and adheres to the sudoku rules
        # if counter is not an allowed value increment counter by 1 and try again
        while puzzleBoard[i][j] ==0:
            if counter < 10:
                row = all([counter != puzzleBoard[i][x] for x in range(9)])
                column = all([counter != puzzleBoard[y][j] for y in range(9)])
                box = all([counter != puzzleBoard[x][y] for x in range(topX, topX+3) for y in range(topY, topY+3)])

                if row and column and box == True:
                    puzzleBoard[i][j]=counter
                    uploadBoard()
                else:
                    counter = counter + 1

            # if counter is larger than ten set the previous square ([i][j-1]) equal to zero, set the counter equal to one more than the previous squares value, and call the solvePuzzle function again.
            else:
                for k in range(i,0,-1):
                    for l in range(j-1,0,-1):
                        if puzzle[k][l]==0:
                            counter = puzzleBoard[k][l] + 1
                            puzzleBoard[k][l]=0
                            solvePuzzle()
                            return
                        else:
                            print("no")

Tags: andthe函数inforifiscounter
1条回答
网友
1楼 · 发布于 2024-05-12 19:11:43

我找到了答案。代码有一些问题,但主要问题是在较低的else语句中counter = puzzleBoard[k][l] + 1,然后再次调用函数,这将变量counter重置为1。你知道吗

我可以通过创建一个全局变量countholder并修改else语句来解决这个问题

完整的工作代码如下所示:

puzzleBoard =[[0,2,0,0,0,0,0,0,0],[0,0,0,6,0,0,0,0,3],
              [0,7,4,0,8,0,0,0,0],[0,0,0,0,0,3,0,0,2],
              [0,8,0,0,4,0,0,1,0],[6,0,0,5,0,0,0,0,0],
              [0,0,0,0,1,0,7,8,0],[5,0,0,0,0,9,0,0,0],
              [0,0,0,0,0,0,0,4,0]]

puzzle =[[0,2,0,0,0,0,0,0,0],[0,0,0,6,0,0,0,0,3],
              [0,7,4,0,8,0,0,0,0],[0,0,0,0,0,3,0,0,2],
              [0,8,0,0,4,0,0,1,0],[6,0,0,5,0,0,0,0,0],
              [0,0,0,0,1,0,7,8,0],[5,0,0,0,0,9,0,0,0],
              [0,0,0,0,0,0,0,4,0]]

countholder = 1

def solvePuzzle():

    #start by looking at each square in the 9x9 sudoku grid and check if that square is empty (=0)
    for i in range(9):
        for j in range(9):
            global countholder
            counter = countholder
            topX = 3*(i//3)
            topY = 3*(j//3)

            # while the board at index [i][j] is empty check if the value of 'counter' fits in the square and adheres to the sudoku rules
            # if counter is not an allowed value increment counter by 1 and try again
            while puzzleBoard[i][j] ==0:
                if counter < 10:
                    row = all([counter != puzzleBoard[i][x] for x in range(9)])
                    column = all([counter != puzzleBoard[y][j] for y in range(9)])
                    box = all([counter != puzzleBoard[x][y] for x in range(topX, topX+3) for y in range(topY, topY+3)])

                    if (row and column and box) == True:
                        puzzleBoard[i][j]=counter
                        print(puzzleBoard)
                        countholder = 1
                    else:
                        counter = counter + 1

                # if counter is larger than ten set the previous square ([i][j-1]) equal to zero, set the counter equal to one more than the previous squares value, and call the solvePuzzle function again.
                else:
                    run_One = True         
                    for k in range(i,-1,-1):
                        if run_One == True:
                            run_One = False
                            for l in range(j,0,-1):
                                if l == 0:
                                    print(run_One)
                                elif puzzle[k][l-1]==0:
                                    countholder = puzzleBoard[k][l-1] + 1
                                    puzzleBoard[k][l-1]=0
                                    solvePuzzle()
                                    return
                                else:
                                    continue
                        else:
                            for l in range(8,-1,-1):
                                if puzzle[k][l]==0:
                                    countholder = puzzleBoard[k][l] + 1
                                    puzzleBoard[k][l]=0
                                    solvePuzzle()
                                    return
                                else:
                                    continue

solvePuzzle()   

相关问题 更多 >