Finding solution to a crypt equation in python
<p>Consider the equation ABCBA = D * BE * BFFA.The task is to determine where A, B, C, D, E and F which are distinct digits. The equation with numerical values is 91819 = 7 * 13 * 1009, hence the program should print {'A':9, 'B':1, 'C':8 , 'D':7,'E':3, 'F':0}.
Here is the code i did</p>

<pre><code>result=[]
for A in range (10):
    for B in range(10):
        for C in range(10):
            for D in range(10):
                for E in range(10):
                   for F in range(10):
                       if int(str(A)+str(B)+str(C)+str(B)+str(A)) == D * int(str(B)+str(E)) * int(str(B)+str(F)+str(F)+str(A)):
                             result.append({'A':A,'B':B,'C':C,'D':D,'E':E,'F':F})
print((result[len(result)-1]))
</code></pre>

<p>Now that it works for the equation in the example.How sholud I modify my code so that it works for any equation,which is to be input by the user?</p>

<p>I don't think @Khris's answer is 100% correct, as you do need a loop for <code>A</code>, and your original question listed that first digit of any number could not be 0; hence <code>A</code>, <code>B</code>, and <code>D</code> cannot be 0, so <code>if A and D and B:</code> is added for this.</p>

<p>which then results in (printing all results, you can pick from there):</p>

<pre><code>from pprint import pprint

results = []
for A in range(10):
    for B in range(10):
        for C in range(10):
            for D in range(10):
                for E in range(10):
                    for F in range(10):
                        if A and D and B:
                            if int(str(A)+str(B)+str(C)+str(B)+str(A)) == D * int(str(B)+str(E)) * int(str(B)+str(F)+str(F)+str(A)):
                                results.append({'A':A,'B':B,'C':C,'D':D,'E':E,'F':F})

for result in results:
    pprint(result)
</code></pre>

<p>You further state the following:</p>

<blockquote>
  <p>The last of many outputs of pprint gives the correct result.Can
  somebody edit it to print only the last(correct) result?</p>
</blockquote>

<p>Which is incorrect based on the equation <code>ABCBA = D * BE * BFFA</code>: the following results would all be valid:</p>

<pre><code>{'A': 1, 'B': 1, 'C': 0, 'D': 1, 'E': 1, 'F': 0}  # 11011 = 1 * 11 * 1001 
{'A': 2, 'B': 1, 'C': 3, 'D': 1, 'E': 6, 'F': 3}  # 21312 = 1 * 16 * 1332 
{'A': 2, 'B': 1, 'C': 9, 'D': 1, 'E': 1, 'F': 9}  # 21912 = 1 * 11 * 1992 
{'A': 5, 'B': 1, 'C': 3, 'D': 3, 'E': 1, 'F': 5}  # 51315 = 3 * 11 * 1555 
{'A': 9, 'B': 1, 'C': 8, 'D': 7, 'E': 3, 'F': 0}  # 91819 = 7 * 13 * 1009
</code></pre>

python python-3.x cryptography combinations