Python function gets stuck (no error) but I can't understand why
<p>The function does what I want it to, but when it's done it just sits there rather than continuing from where I called it and I can't figure out why. The code is:</p>

<pre><code>x = 9
y = 9
n = 10
ty = 1
tx = 1

while ty &lt;= y:
    while tx &lt;= x:
        vars()["p" + str(ty) + str(tx)] = 0
        tx += 1
    ty += 1
    tx = 1

tn = 1
while tn &lt;= n:
    vars()["m" + str(tn)] = tn
    tn += 1

t = x * y
tm = n
def recursion(z):
    global tm
    if z &lt; n:
        for x in range(n):
            recursion(z + 1)
    else:
        if tm &gt; 0:
            tv = "m" + str(tm)
            otm = eval(tv)
            while eval(tv) &lt; t - n + tm:
                vars()[tv] = eval(tv) + 1
                print(tv + " = " + str(eval(tv)))
            vars()[tv] = otm + 1
            print(tv + " = " + str(eval(tv)))
            if tm &gt; 1:
                vars()["m" + str(tm - 1)] = eval("m" + str(tm - 1)) + 1
                print(str("m" + str(tm - 1) + " = " + str(eval("m" + str(tm -1)))))
            tm -= 1

recursion(1)

print("done")
</code></pre>

<p>I've put the return in where I would expect it to end but as far as I know it shouldn't actually need it. </p>

<p>Can anyone see what I've done to cause it to get stuck?</p>

<p>Thanks</p>

<p>Note for people not going through the change history: This is based on the comments on other answers. UPDATE: Better version.</p>

<pre><code>import itertools

def permutations(on, total):
    all_indices = range(total)
    for indices in itertools.combinations(all_indices, on):
        board = ['0'] * total
        for index in indices:
            board[index] = '1'
        yield ''.join(board)
</code></pre>

python python-3.x