循环没有完成它的任务

时间:2017-08-09 17:37:23

标签: python arrays

我正在尝试创建一个数独谜题,所以我想出了下面的这个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)

1 个答案:

答案 0 :(得分: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中填写什么?没有什么可能,而循环将被卡住。