簡體   English   中英

為什么我的python程序中的while循環沒有結束?

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

我是一名新手程序員,正在學習 Python 作為第一語言。 為了練習,我制作了一個程序來生成一個完整的數獨謎題,希望以后可以作為帶有 GUI 的數獨游戲的一部分使用。 但截至目前,它什么也沒做。 我的意思是當它運行時,屏幕上不會打印任何內容。 我已經檢查以確保所有內容的格式都正確,並且據我所知,我的所有語法都是正確的。 是否有一些邏輯錯誤沒有引起我的注意? 無論如何,我懷疑有什么東西阻止了我的 while 循環中斷。 此外,我意識到我的程序可能太長了,但由於我才剛剛開始學習該程序,因此我可以使用的工具包非常有限。

這是我的整個程序:

    # 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.")

您的代碼將永遠運行,因為如果您進入無法解決的棋盤狀態,您的算法中沒有任何內容可以使其回溯。 例如,考慮該板前兩行的值:

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

到目前為止輸入的數字都是合法的,因為它們在行、列或框中不重疊。 但是沒有合法值可以放在第二行的最后一列。 如果您的代碼在此模式中隨機放置前 17 個數字,則在嘗試放置第 18 個數字時將永遠卡住。 在卡住之前,您的程序很可能比這更進一步,但卡住的可能性可能比生成一個完整的棋盤要大(我不確定概率是如何計算的)。

暫無
暫無

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

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