简体   繁体   English

为什么我的python程序中的while循环没有结束?

[英]Why does the while loop in my python program not end?

I am a novice programmer and am learning python as a first language.我是一名新手程序员,正在学习 Python 作为第一语言。 For practice I made a program that generates a completed sudoku puzzle, hopefully to be used later as part of a sudoku game with a GUI.为了练习,我制作了一个程序来生成一个完整的数独谜题,希望以后可以作为带有 GUI 的数独游戏的一部分使用。 But as of right now it does nothing.但截至目前,它什么也没做。 And by that I mean when it is run nothing is printed out on the screen.我的意思是当它运行时,屏幕上不会打印任何内容。 I've checked to make sure everything is formatted correctly, and to the best of my understanding all of my syntax is correct.我已经检查以确保所有内容的格式都正确,并且据我所知,我的所有语法都是正确的。 Is there some logical error that has escaped my notice?是否有一些逻辑错误没有引起我的注意? In any case, I suspect that something is preventing my while loop from breaking.无论如何,我怀疑有什么东西阻止了我的 while 循环中断。 Also, I realize that my program is probably unnecessarily long, but since I am still only beginning to learn the program I had a very limited toolkit to work with.此外,我意识到我的程序可能太长了,但由于我才刚刚开始学习该程序,因此我可以使用的工具包非常有限。

Here is my program in its entirety:这是我的整个程序:

    # Sudoku puzzle maker
    # Practice game that makes an unfinished sudoku puzzle for the player    to solve

    import random

    # The readout string stores the assigned numbers given to each square
    # in the puzzle.

    readout = ""

    # The "c" strings store the numbers contained in each of the columns.
    # The program will check these strings to make sure that only one of each
    # number from 1 to 9 is in each column.
    # The same is true for the "r" (row) and "b" (box) strings.

    c1 = ""
    c2 = ""
    c3 = ""
    c4 = ""
    c5 = ""
    c6 = ""
    c7 = ""
    c8 = ""
    c9 = ""

    r1 = ""
    r2 = ""
    r3 = ""
    r4 = ""
    r5 = ""
    r6 = ""
    r7 = ""
    r8 = ""
    r9 = ""

    b1 = ""
    b2 = ""
    b3 = ""
    b4 = ""
    b5 = ""
    b6 = ""
    b7 = ""
    b8 = ""
    b9 = ""

    # The "square" strings store the location of each square as a 3-digit code,
    # the first digit is the column, the second the row, and the third the box.
    # For example, the fourth square is in c4 (column 4), r1 (row 1),
    # and b2 (3x3 box 2), so its corresponding string is "412".

    square_1 = "111"
    square_2 = "211"
    square_3 = "311"
    square_4 = "412"
    square_5 = "512"
    square_6 = "612"
    square_7 = "713"
    square_8 = "813"
    square_9 = "913"
    square_10 = "121"
    square_11 = "221"
    square_12 = "321"
    square_13 = "422"
    square_14 = "522"
    square_15 = "622"
    square_16 = "723"
    square_17 = "823"
    square_18 = "923"
    square_19 = "131"
    square_20 = "231"
    square_21 = "331"
    square_22 = "432"
    square_23 = "532"
    square_24 = "632"
    square_25 = "733"
    square_26 = "833"
    square_27 = "933"
    square_28 = "144"
    square_29 = "244"
    square_30 = "344"
    square_31 = "445"
    square_32 = "545"
    square_33 = "645"
    square_34 = "746"
    square_35 = "846"
    square_36 = "946"
    square_37 = "154"
    square_38 = "254"
    square_39 = "354"
    square_40 = "455"
    square_41 = "555"
    square_42 = "655"
    square_43 = "756"
    square_44 = "856"
    square_45 = "956"
    square_46 = "164"
    square_47 = "264"
    square_48 = "364"
    square_49 = "465"
    square_50 = "565"
    square_51 = "665"
    square_52 = "766"
    square_53 = "866"
    square_54 = "966"
    square_55 = "177"
    square_56 = "277"
    square_57 = "377"
    square_58 = "478"
    square_59 = "578"
    square_60 = "678"
    square_61 = "779"
    square_62 = "879"
    square_63 = "979"
    square_64 = "187"
    square_65 = "287"
    square_66 = "387"
    square_67 = "488"
    square_68 = "588"
    square_69 = "688"
    square_70 = "789"
    square_71 = "889"
    square_72 = "989"
    square_73 = "197"
    square_74 = "297"
    square_75 = "397"
    square_76 = "498"
    square_77 = "598"
    square_78 = "698"
    square_79 = "799"
    square_80 = "899"
    square_81 = "999"

    # The "master_list" is a tuple that stores all of the "square" strings in order
    # of their appearance. I realize now that I could have just had it store the
    # strings directly, but I'm too lazy to go back and change it.

    master_list = (square_1, square_2, square_3, square_4, square_5, square_6, square_7, square_8, square_9,\

     square_10, square_11, square_12, square_13, square_14, square_15, square_16, square_17, square_18,\

     square_19, square_20, square_21, square_22, square_23, square_24, square_25, square_26, square_27,\

     square_28, square_29, square_30, square_31, square_32, square_33, square_34, square_35, square_36,\

     square_37, square_38, square_39, square_40, square_41, square_42, square_43, square_44, square_45,\

     square_46, square_47, square_48, square_49, square_50, square_51, square_52, square_53, square_54,\

     square_55, square_56, square_57, square_58, square_59, square_60, square_61, square_62, square_63,\

     square_64, square_65, square_66, square_67, square_68, square_69, square_70, square_71, square_72,\

     square_73, square_74, square_75, square_76, square_77, square_78, square_79, square_80, square_81)

    # This for loop, for each square, picks a random number, converts it from an
    # integer into a string, and checks to see if it is already in the same column,
    # row, or box. It checks this by deciding which "c" "r" and "b" string
    # to look into based on the 3-digit code in each "square" string, and if
    # it finds that the number it picked is already in the same c, r, or b, it
    # tries again with a new random number. If the number fits, it is added to
    # the proper "c", "r", and "b" strings for future reference, and added to the
    # "readout" string.

    for square in master_list:
        while True:
            number = str(random.randint)

    # Here the program finds which column the square is in, and checks that column.      

            if square[0] == "1":
                column = c1
                if number in c1:
                    continue
            elif square[0] == "2":
                column = c2
                if number in c2:
                    continue
            elif square[0] == "3":
                column = c3
                if number in c3:
                    continue
            elif square[0] == "4":
                column = c4
                if number in c4:
                    continue
            elif square[0] == "5":
                column = c5
                if number in c5:
                    continue
            elif square[0] == "6":
                column = c6
                if number in c6:
                    continue
            elif square[0] == "7":
                column = c7
                if number in c7:
                    continue
            elif square[0] == "8":
                column = c8
                if number in c8:
                    continue
            elif square[0] == "9":
                column = c9
                if number in c9:
                    continue

    #Here the program finds which row it is in and checks the row.


            if square[1] == "1":
                row = r1
                if number in r1:
                    continue
            elif square[1] == "2":
                row = r2
                if number in r2:
                    continue
            elif square[1] == "3":
                row = r3
                if number in r3:
                    continue
            elif square[1] == "4":
                row = r4
                if number in r4:
                    continue
            elif square[1] == "5":
                row = r5
                if number in r5:
                    continue
            elif square[1] == "6":
                row = r6
                if number in r6:
                    continue
            elif square[1] == "7":
                row = r7
                if number in r7:
                    continue
            elif square[1] == "8":
                row = r8
                if number in r8:
                    continue
            elif square[1] == "9":
                row = r9
                if number in r9:
                    continue

    #Here it finds which box it is in and checks the box.


            if square[2] == "1":
                box = b1
                if number in b1:
                    continue
            elif square[2] == "2":
                box = b2
                if number in b2:
                    continue
            elif square[2] == "3":
                box = b3
                if number in b3:
                    continue
            elif square[2] == "4":
                box = b4
                if number in b4:
                    continue
            elif square[2] == "5":
                box = b5
                if number in b5:
                    continue
            elif square[2] == "6":
                box = b6
                if number in b6:
                    continue
            elif square[2] == "7":
                box = b7
                if number in b7:
                    continue
            elif square[2] == "8":
                box = b8
                if number in b8:
                    continue
            elif square[2] == "9":
                box = b9
                if number in b9:
                    continue


    # If a random number has gotten this far, it means it has passed inspection.
    # Now the program concatenates the number to the correct "c", "r" and "b"
    # strings for future reference.


            if column == c1:
                    c1 += number
            elif column == c2:
                    c2 += number
            elif column == c3:
                    c3 += number
            elif column == c4:
                    c4 += number
            elif column == c5:
                    c5 += number
            elif column == c6:
                    c6 += number
            elif column == c7:
                    c7 += number
            elif column == c8:
                    c8 += number
            elif column == c9:
                    c9 += number

            if row == r1:
                    r1 += number
            elif row == r2:
                    r2 += number
            elif row == r3:
                    r3 += number
            elif row == r4:
                    r4 += number
            elif row == r5:
                    r5 += number
            elif row == r6:
                    r6 += number
            elif row == r7:
                    r7 += number
            elif row == r8:
                    r8 += number
            elif row == r9:
                    r9 += number

            if box == b1:
                    b1 += number
            elif box == b2:
                    b2 += number
            elif box == b3:
                    b3 += number
            elif box == b4:
                    b4 += number
            elif box == b5:
                    b5 += number
            elif box == b6:
                    b6 += number
            elif box == b7:
                    b7 += number
            elif box == b8:
                    b8 += number
            elif box == b9:
                    b9 += number

    # Now the number is added to the readout and the while loop breaks, moving
    # the for loop on to the next square.

            readout += number
            break

    print(readout)



    input("\n\nyay it worked.")

Your code runs forever because there's nothing in your algorithm that can make it backtrack if you get into an unsolvable board state.您的代码将永远运行,因为如果您进入无法解决的棋盘状态,您的算法中没有任何内容可以使其回溯。 For an example, consider the values on the first two rows of this board:例如,考虑该板前两行的值:

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

The numbers that have been entered so far are all legal, since they don't overlap in row, column or box.到目前为止输入的数字都是合法的,因为它们在行、列或框中不重叠。 But there's no legal value that can be put in the last column of the second row.但是没有合法值可以放在第二行的最后一列。 If your code randomly placed the first 17 numbers in this pattern, it would be stuck forever trying to place the 18th.如果您的代码在此模式中随机放置前 17 个数字,则在尝试放置第 18 个数字时将永远卡住。 Most likely your program gets a bit further than this before getting stuck, but it may be more likely to get stuck than to generate a full board (I'm not sure how the probabilities work out).在卡住之前,您的程序很可能比这更进一步,但卡住的可能性可能比生成一个完整的棋盘要大(我不确定概率是如何计算的)。

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

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