cnt = 1 def solve(n, A, B, C): global cnt if n > 0: solve(n-1, A, C, B) if A[0]: disc = A[0].pop() print '%2d: move %d from %s to %s'%(cnt, disc, A[1], C[1]) cnt += 1 C[0].append(disc) solve(n-1, B, A, C) def hanoi(n): A = (range(1, n+1)[::-1], 'source') B = ([], 'helper') C = ([], 'target') solve(n, A, B, C) hanoi(4)