Python While循环被忽略

时间:2015-08-13 18:15:02

标签: python python-2.7 while-loop

我是python的学生,我正在开发这个代码来计算3'A'到'9'的所有组合可能性,直到OriginalArray变得与PerfectArray相等......但由于某种原因,while循环不停。任何人都知道为什么?谢谢你的帮助:D

#!/usr/bin/python2.7

OriginalArray = ['1','A','A','A']

def CombineLastThree(Array):
    values = "ABCDEFGHIJKLMNOPQRSTUVWXYZ0123456789"

    for a in values:
        Array[1] = a
            for b in values:
            Array[2] = b
            for c in values:
                Array[3] = c
                print (Array)

def Run(Array):
    PerfectArray2 = ['2','A','A','A']

    while Array != PerfectArray2:
        CombineLastThree(Array)
        if Array[1] == '9' and Array[2] == '9' and Array[3] == '9':
            Array[1] = 'A'
            Array[2] = 'A'
            Array[3] = 'A'
            Array[0] = int(Array[0]) + 1

Run(OriginalArray)

1 个答案:

答案 0 :(得分:3)

Array[0]中有一个整数。这永远不会等于'2'。这可能会解决它:

Array[0] = str(int(Array[0]) + 1)

您可能还想查看itertools模块: https://docs.python.org/2/library/itertools.html#itertools.combinations

这不完全是您的代码所做的,但可能值得考虑作为起点:

from itertools import combinations

values = "ABCDEFGHIJKLMNOPQRSTUVWXYZ0123456789"

for a, b, c in combinations(values, 3):
    print(a,b,c)