Python - reducing long if-elif lines of code
<p>I have just started learning python, and while my code is working, I want to reduce my lines of code. I was thinking of using list method, but I couldn't really come up of a solution. I tried searching beforehand, but I couldn't find something related to mine.</p>

<p>For my code, it's like a point moving through one space to another. The possible movements are determined by directions. What I did to determine the movement of the point was to assign a <code>userPoint</code> (which determines where the point is). In order to move, it has to satisfy the conditions set by the space (aka the only available directions represented by <code>userInput.upper()</code>) or else it will not move and print that the input was invalid.</p>

<pre><code>if userInput.upper() == 'QUIT':
    break
elif userPoint == 0 and userInput.upper() =='EAST':
    userPoint = 1
elif userPoint == 1 and userInput.upper() == 'WEST':
    userPoint = 0
elif userPoint == 1 and userInput.upper() == 'EAST':
    userPoint = 2
elif userPoint == 1 and userInput.upper() == 'SOUTH':
    userPoint = 4
elif userPoint == 2 and userInput.upper() == 'WEST':
    userPoint = 1
elif userPoint == 3 and userInput.upper() == 'SOUTH':
    userPoint = 6
elif userPoint == 4 and userInput.upper() == 'NORTH':
    userPoint = 1
elif userPoint == 4 and userInput.upper() == 'EAST':
    userPoint = 5
elif userPoint == 5 and userInput.upper() == 'WEST':
    userPoint = 4
elif userPoint == 5 and userInput.upper() == 'SOUTH':
    userPoint = 8
elif userPoint == 6 and userInput.upper() == 'NORTH':
    userPoint = 3
elif userPoint == 6 and userInput.upper() == 'EAST':
    userPoint = 7
elif userPoint == 7 and userInput.upper() == 'WEST':
    userPoint = 6
elif userPoint == 7 and userInput.upper() == 'EAST':
    userPoint = 8
elif userPoint == 8 and userInput.upper() == 'WEST':
    userPoint = 7
elif userPoint == 8 and userInput.upper() =='NORTH':
    userPoint = 5
else:
    print('Please input a valid direction.\n')
</code></pre>

<p>Thank you very much for the help!</p>

<p>I'll do it by creating a dictionary mapping
Something like this</p>

<pre><code>mapping = {
    0 : {
        "EAST" : 1
    },
    1 : {
        "EAST": 2,
        "WEST": 1,
        "SOUTH": 4
    }
}


if userInput.upper() == 'QUIT':
    break
else:
    userInput = mapping[userPoint][userInput.upper()]
</code></pre>

python python-3.x