简体   繁体   English

Python 问题,用 True 和 False 在循环中检查和填充 2D 列表

[英]Python problem with checking and filling 2D-list in a loop with True and False

so I have a function generateSudoku() which should check my 2 Dimensional List which goes from [0][0] to [8][8] (81 fields), and fill in the fields with random numbers by following the rules of a sudoku game which is checked by kontrolleFeld()所以我有一个函数generateSudoku()它应该检查我的二维列表,它从 [0][0] 到 [8][8](81 个字段),并按照以下规则用随机数填写字段由kontrolleFeld()检查的数独游戏

kontrolleFeld() checks if the random generated number fits in to the field. kontrolleFeld()检查随机生成的数字是否适合该字段。 If kontrolleFeld() is true then it goes on to the next field to check and fill in a number up to field [0][8] and then it starts from the next "row"( [1][0] ) to check so that it should check up all fields up to [8][8].如果kontrolleFeld()为真,则继续到下一个字段检查并填写一个直到字段 [0][8] 的数字,然后从下一个“行”( [1][0] )开始检查因此它应该检查所有字段,直到 [8][8]。 If its false then it checks the same field again with another int number.如果它为假,那么它会用另一个整数再次检查相同的字段。

but something is unfortunately wrong because sometimes it checks up to 2nd row and stops sometimes 3rd row and stops and sometimes only 1st row.但不幸的是,有些事情是错误的,因为有时它会检查到第 2 行,有时会停止第 3 行并停止,有时只会停止第 1 行。

the kontrolleFeld() function which gives back True or False is working flawless.返回 True 或 False 的kontrolleFeld()函数运行完美。 This isn't the problem.这不是问题。 (I tested it with already given sudoku) (我用已经给出的数独对其进行了测试)

def generateSudoku():
    xcount = 0
    ycount = 0
    while xcount !=9:
        if ycount == 9:
            xcount = xcount +1
            ycount = 0
        else:
            while ycount !=9:
                zrand = random.randint(1,9)
                if kontrolleFeld(xcount,ycount,zrand):
                    s1[xcount][ycount]=zrand
                    ycount = ycount +1

Suppose the first 2 rows of your grid have been filled in as follows:假设您的网格的前 2 行已按如下方式填写:

1 2 3 4 5 6 7 8 9
4 5 6 1 2 3 ?

There is no value that can legally replace the ?, so your while True loop will never terminate.没有任何值可以合法地替换 ?,因此您的while True循环将永远不会终止。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM