2016-04-05 68 views
-1

我是一個新手程序員,正在學習python作爲第一語言。 實踐中,我製作了一個程序,可以生成完成的數獨謎題,希望稍後可以作爲數獨遊戲的一部分使用。但就目前而言,它什麼都不做。我的意思是當它運行時屏幕上沒有任何東西被打印出來。我已經檢查過,以確保所有內容格式正確,並且盡我所能理解我的所有語法都是正確的。是否有一些邏輯錯誤已經逃脫我的注意?無論如何,我懷疑有些東西正在阻止我的while循環打破。另外,我意識到我的程序可能不必要的長,但由於我還只是開始學習該程序,所以我有一個非常有限的工具包可以使用。爲什麼我的python程序中的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.") 
+0

當事情不順利時,一個很好的調試技巧就是創建一個簡化的問題示例。在這種情況下,你可以嘗試創建一個2x2數獨謎題。 –

+0

我甚至無法忍受閱讀這段代碼,有*方式*太多的變量。使用列表和索引等數據結構比使用名稱中的數字不同的幾十個變量要好得多。 – Blckknght

+0

'str(random.randint)'在0x2402380處返回

回答

0

您的代碼將永遠運行下去,因爲沒有什麼在你的算法,可以讓你進入一個無法解決的板狀態時,它原路返回。舉一個例子,可以考慮在此板前兩行的值:

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

已經輸入到目前爲止都是合法的,因爲他們不行,列或箱重疊的數字。但是沒有合法價值可以放在第二行的最後一列。如果你的代碼在這個模式中隨機放置了前17個數字,它將永遠停留在試圖放置第18個數字的位置。很可能你的程序在被卡住之前會比這更進一步,但它可能比卡片更容易卡住(我不確定這些概率是如何產生的)。

+0

這是一個很好的見解,但我不確定這是否解釋了代碼無法工作的原因。如果刪除干預的if-else,則代碼在master_list中簡化爲'for square:while True:number = str(random.randint);讀數+ =數字; break'。分號表示換行符,假定縮進發生在結腸上。 至少應該打印出_something_。 –

+0

不,如果沒有像我描述的那樣不可能的電路板情況,算法的整體理論可以工作。你跳過的'if' /'elif'塊的後半部分做了一些工作:每找到一個有效的移動就修改一個'cN','rN'和'bN'變量。如果可以找到,那麼'readout'就是完成的板子。 – Blckknght

+0

非常感謝你們倆。我添加了一些功能來解決程序遇到不可能的主板情況的問題,但它仍然無法正常工作。它只是說在運行時「重啓」。其他人也遇到過這個問題,事實證明,這個問題顯然源於沒有建立正確的文件路徑。同樣,感謝您的洞察力,我一定會發布最終的代碼,並說明我如何運作。 –