2017-08-09 249 views
2

我想創建一個Sudoku拼圖,所以我想出了這個python代碼,下面是打印一個字典,其鍵是從Btns列表,但是當運行代碼沒有完成該任務也不會給出任何錯誤消息。我需要幫助解密問題可能是什麼以及如何解決問題。循環沒有完成它的任務

from random import randint 

    Btns = ['A11', 'A12', 'A13', 'A21', 'A22', 'A23', 'A31', 'A32', 'A33', 
      'B14', 'B15', 'B16', 'B24', 'B25', 'B26', 'B34', 'B35', 'B36', 
      'C17', 'C18', 'C19', 'C27', 'C28', 'C29', 'C37', 'C38', 'C39', 
      'D41', 'D42', 'D43', 'D51', 'D52', 'D53', 'D61', 'D62', 'D63', 
      'E44', 'E45', 'E46', 'E54', 'E55', 'E56', 'E64', 'E65', 'E66', 
      'F47', 'F48', 'F49', 'F57', 'F58', 'F59', 'F67', 'F68', 'F69', 
      'G71', 'G72', 'G73', 'G81', 'G82', 'G83', 'G91', 'G92', 'G93', 
      'H74', 'H75', 'H76', 'H84', 'H85', 'H86', 'H94', 'H95', 'H96', 
      'I77', 'I78', 'I79', 'I87', 'I88', 'I89', 'I97', 'I98', 'I99'] 
    H1 = list(range(1,10)) 
    H2 = list(range(1,10)) 
    H3 = list(range(1,10)) 
    H4 = list(range(1,10)) 
    H5 = list(range(1,10)) 
    H6 = list(range(1,10)) 
    H7 = list(range(1,10)) 
    H8 = list(range(1,10)) 
    H9 = list(range(1,10)) 
    V1 = list(range(1,10)) 
    V2 = list(range(1,10)) 
    V3 = list(range(1,10)) 
    V4 = list(range(1,10)) 
    V5 = list(range(1,10)) 
    V6 = list(range(1,10)) 
    V7 = list(range(1,10)) 
    V8 = list(range(1,10)) 
    V9 = list(range(1,10)) 
    A = list(range(1,10)) 
    B = list(range(1,10)) 
    C = list(range(1,10)) 
    D = list(range(1,10)) 
    E = list(range(1,10)) 
    F = list(range(1,10)) 
    G = list(range(1,10)) 
    H = list(range(1,10)) 
    I = list(range(1,10)) 

    the_game = {} 

    #iterating through Btns 
    for btns in Btns: 
      first = btns[0] 
      second = btns[1] 
      third = btns[2] 
      r = randint(1, 9) 

      #Assigning an array to first_list 
      if first == 'A': 
        first_list = A 
      elif first == 'B': 
        first_list = B 
      elif first == 'C': 
        first_list = C 
      elif first == 'D': 
        first_list = D 
      elif first == 'E': 
        first_list = E 
      elif first == 'F': 
        first_list = F 
      elif first == 'G': 
        first_list = G 
      elif first == 'H': 
        first_list = H 
      else: 
        first_list = I 

      #Assigning an array to second_list 
      if second == '1': 
        second_list = H1 
      elif second == '2': 
        second_list = H2 
      elif second == '3': 
        second_list = H3 
      elif second == '4': 
        second_list = H4 
      elif second == '5': 
        second_list = H5 
      elif second == '6': 
        second_list = H6 
      elif second == '7': 
        second_list = H7 
      elif second == '8': 
        second_list = H8 
      else: 
        second_list = H9 

      #Assigning an array to third_list 
      if third == '1': 
        third_list = V1 
      elif third == '2': 
        third_list = V2 
      elif third == '3': 
        third_list = V3 
      elif third == '4': 
        third_list = V4 
      elif third == '5': 
        third_list = V5 
      elif third == '6': 
        third_list = V6 
      elif third == '7': 
        third_list = V7 
      elif third == '8': 
        third_list = V8 
      else: 
        third_list = V9 

      #Making sure r is contained in the three lists 
      while (r not in first_list) or (r not in second_list) or (r not in third_list): 
        r = randint(1, 9) 

      print(r) 
      print(first_list) 
      print(second_list) 
      print(third_list) 
      the_game[btns] = r # Adding r to the dictionary 

      #Removing r from the three lists 
      first_list.remove(r) 
      second_list.remove(r) 
      third_list.remove(r) 
    print(the_game) 
+0

想一想,如果你需要這麼多的實例變量重複。你可以用一個數組來完成這個任務。 – Ares

+0

@Ekene,歡迎來到Stack Overflow!你的代碼中有一個「無限循環」。工作是找出它是在外部的「for」還是在內部的「while」循環中。我建議你縮小你的嘗試。首先,做一個9元素Btns陣列;然後爲原來的81個元素做。這是迪克斯特拉在工作中的分而治之的方法。希望能幫助到你。好的編碼! – ranieribt

回答

0

可以有當沒有數字的從1到9存在於所有列表的情形:

A11:1,A12:2,A13:3,A21:4,A22: 5,A23:6,A31:7,A32:8,A33:9

B15:6,B25:2,B35:1

D51:3,D52:4,D53:5

E44:7,E45:8,E46:9

現在可以在E55中填充什麼?沒有可能,while循環將被卡住。