How to use recursion in python
<p>I am trying to solve a problem which stimulate movements of a robot. The robot starts with position (0, 0, 'N'). The command is given in a list of strings. the 'turn' function turns from N to E to S to W and back to N. The move function moves in the specific direction: N,S in y axis and E,W in x axis. N: y+1 S: y-1 W: x-1 E: x+1</p>

<p>The part I am having trouble with is, when trying to use shortcuts in the function. Using 'turnleft' instead of ['turn', 'turn', 'turn'], 'turnright' instead of 'turn'</p>

<pre><code>def macro_interpreter(code, macros):
</code></pre>

<p>when call the function:</p>

<pre><code>print(macro_interpreter(['turnleft', 'turnright'], {'turnleft': ['turn', 'turn', 'turn'], 'turnright' : ['turn'], 'bigleftturn' : ['move', 'move', 'turnleft', 'move', 'move'], 'bigrightturn' : ['move', 'move', 'turnright', 'move', 'move']}))
</code></pre>

<p>the definition of the term is given in a dictionary.
My code only runs for the first command and then terminate, it ignores the second code in the list</p>

<pre><code>def macro_interpreter(code, macros):
    x,y,index = 0, 0, 0
    state = ['N', 'E', 'S', 'W']
    for command in code:
        if command in macros:
            return macro_interpreter(macros[command], macros)
        else:
            if command == 'move':
                if state[index] == 'N':
                    y += 1
                elif state[index] == 'E':
                    x += 1
                elif state[index] == 'S':
                    y -= 1
                elif state[index] == 'W':
                    x -= 1
            elif command == 'turn':
                try:
                    index = index + 1
                except IndexError:
                    index = 0
    return (x, y, state[index])            
</code></pre>

<p>If you always hit the one <code>else</code> statement in the loop over the <code>code</code> commands, then you never will recurse because  <code>command not in macros</code>. </p>

<p>After the first iteration, <code>code == ['turn', 'turn', 'turn']</code>, but <code>macros</code> contains no key <code>"turn"</code></p>

<hr>

<p>If you wish to do this more correctly, then you can pass along x, y, and the "direction index state" as parameters to the function, then increment / modify those within the recursive call rather than only modify local variables of the function and always restart them back at (0,0, 0). </p>

<p>Also, you need to replace that try except with <code>index = (index + 1) % len(state)</code> because no IndexError is going to be caught by incrementing a number </p>

<hr>

<p>So, something like this </p>

<pre><code>state = ['N', 'E', 'S', 'W']
def macro_interpreter(code, macros, x=0,y=0,index=0):
    # TODO: check if x or y have gone outside "the board" 
        # TODO: return to break from recursion 

    for command in code:
        if command in macros:
            return macro_interpreter(macros[command], macros,x,y,index)
        else:
            if command == 'move':
                if state[index] == 'N':
                    return macro_interpreter(code[1:], macros,x,y=y+1,index)
</code></pre>

python python-3.x