Python 中的乘法数字求解器

Multiplication Digits Solver in Python

我正在 Python 中创建一个程序,用于查找可以匹配以下乘法字母的数字,例如:

奥托 x
____I
停止
(意思是 OTTO x I = STOP)

这是我一直在处理的代码,但它似乎并没有停止...

# OTTO x I = STOP

times = 1
digit_o = 0
digit_t = 0
digit_i = 1
digit_s = 0
digit_p = 0

concatenation_otto = str(str(digit_o) + str(digit_t) + str(digit_t) + str(digit_o))
concatenation_stop = str(str(digit_s) + str(digit_t) + str(digit_o) + str(digit_p))

def amend() :
    concatenation_otto = str(str(digit_o) + str(digit_t) + str(digit_t) + str(digit_o))
    concatenation_stop = str(str(digit_s) + str(digit_t) + str(digit_o) + str(digit_p))
amend()

print "working..."
while (concatenation_otto * digit_i == concatenation_stop) :
    while times == 7 :
        if times == 1 :
            digit_o += 1
            times += 1
            print digit_o + " / " + str(times)
            amend()
            break
        elif times == 2 :
            digit_t += 1
            times += 1
            amend()
            break
        elif times == 3 :
            digit_i += 1
            times += 1
            amend()
            break
        elif times == 4 :
            digit_s += 1
            times += 1
            amend()
            break
        elif times == 5 :
            digit_p += 1
            times += 1
            amend()
            break

        times == 1
        continue

print digit_o
print concatenation_otto + " x " + str(digit_i) + " = " + concatenation_stop

请帮忙,我希望你完全明白我的意思。谢谢!

无需重新发明轮子。使用 itertools.permutations 创建数字的 5 次排列,然后检查结果。

import itertools

for O, T, I, S, P in itertools.permutations((1,2,3,4,5,6,7,8,9), 5):
    OTTO = 1000 * O + 100 * T + 10 * T + O
    STOP = 1000 * S + 100 * T + 10 * O + P
    if OTTO * I == STOP:
        print(O, T, I, S, P)
        break