Warning: file_get_contents(/data/phpspider/zhask/data//catemap/8/python-3.x/18.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Python while循环提前终止_Python_Python 3.x - Fatal编程技术网

Python while循环提前终止

Python while循环提前终止,python,python-3.x,Python,Python 3.x,我正在尝试制作一个Python程序,它将大量生产出一个已解决的数独难题。它随机生成一个磁贴的坐标,如果该磁贴中已经有一个数字,它会重试。然后,它生成一个介于1和9之间的数字放在那里,如果该数字不在该行、列或部分中,它将赋值并将这些坐标添加到已占用的平铺列表中。填充完所有的磁贴后,应该退出循环并返回完成的网格 问题是,在大约70次循环之后,它总是无缘无故地停止,导致程序冻结 下面是我所说的函数的代码: def populate(grid): usedCoords = [] popu

我正在尝试制作一个Python程序,它将大量生产出一个已解决的数独难题。它随机生成一个磁贴的坐标,如果该磁贴中已经有一个数字,它会重试。然后,它生成一个介于1和9之间的数字放在那里,如果该数字不在该行、列或部分中,它将赋值并将这些坐标添加到已占用的平铺列表中。填充完所有的磁贴后,应该退出循环并返回完成的网格

问题是,在大约70次循环之后,它总是无缘无故地停止,导致程序冻结

下面是我所说的函数的代码:

def populate(grid):
    usedCoords = []
    populated = False
    while not populated:
        x = random.randrange(len(grid))
        y = random.randrange(len(grid))
        while [x,y] in usedCoords:
            x = random.randrange(len(grid))
            y = random.randrange(len(grid))
        value = random.randrange(1, len(grid) + 1)
        if not rowCheck(grid, x, y, value) and not columnCheck(grid, x, y, value) and not squareCheck(grid, x, y, value):
            grid[x][y] = value
            usedCoords.append([x,y])
            print(len(usedCoords))
        if len(usedCoords) == len(grid) ** 2:
            populated = True
    return grid
下面是它引用的函数的代码:

def rowCheck(grid, x, y, value):
    for i in range(len(grid)):
        if not i == x:
            if grid[i][y] == value:
                return True
    return False

def columnCheck(grid, x, y, value):
    for i in range(len(grid)):
        if not i==y:
            if grid[x][i] == value:
                return True
    return False

def squareCheck(grid, x, y, value):
    grid2 = [0] * (sectionSide) #new grid for the specific section
    for i in range(len(grid2)):
        grid2[i] = [0] * sectionSide
    for i in range(x - (sectionSide - 1), x + sectionSide): #scanning only nearby coordinates
        for j in range(y - (sectionSide - 1), y + sectionSide):
            try:
                if i // sectionSide == x // sectionSide and j // sectionSide == y // sectionSide:
                    grid2[i][j] = grid[x][y]
            except IndexError:
                pass
    for i in range(len(grid2)):
        for j in range(len(grid2[i])):
            if grid2[i][j] == value and not (i == x and j == y):
                return True
    return False

可能还有其他问题,但您的代码的一个大问题是,如果发现创建了无法解决的板状态,则无法回溯。考虑如果您的代码在板的前两行中放置下列值:

会发生什么?
1 2 3 4 5 6 7 8 9
4 5 6 7 8 1 2 3
到目前为止,已经放置的数字都是合法的,但是没有数字可以放在第二行的最后一个空格中。我猜你的代码最终会被卡住,因为它创建了一堆这样的董事会席位,而这些席位没有任何价值。如果没有合法的行动,它将永远继续循环


您需要更复杂的算法来避免此问题。

您确定循环提前终止,而不是根本不终止吗?