簡體   English   中英

Python for循環范圍函數導致無限循環

[英]Python for loop range function causing an infinite loop

我正在構建一個數獨函數來學習如何在python中編碼。 我似乎用for循環創建了一個無限循環,但我不明白如何。 代碼試圖查看數獨板的每個空方塊,並檢查數獨規則是否允許值counter 如果允許計數器,則更新板並且功能移動到下一個空方塊。 如果不允許計數器,則計數器增加1並再次測試。

我遇到的問題是當計數器大於9時。當發生這種情況時,我想查看原始板上的前一個方塊(命名為難題)並刪除此方塊中的值。 該函數應該設置計數器等於前一個方格+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")

我能夠找到答案。 代碼有一些問題,但主要問題是在較低的else語句counter = puzzleBoard[k][l] + 1然后再次調用該函數,它將變量counter重置為1。

我能夠通過創建一個全局變量countholder並修改else語句來說明countholder = puzzleBoard[k][l] + 1來解決這個問題。

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

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()   

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM