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>It seems like the user is moving through a sort of labyrinth that looks like this:</p>

<pre class="lang-none prettyprint-override"><code>2--1--0
   |
5--4  3
|     |
8--7--6
</code></pre>

<p>We can store the valid connections as a list of sets.  For example, <code>{0, 1}</code> represents a connection that goes from 0 to 1 and from 1 to 0.  Sets have no ordering, so <code>{0, 1}</code> and <code>{1, 0}</code> are the same thing.</p>

<pre><code>if userInput.upper() == 'QUIT':
    break

connections = [{0,1}, {1,2}, {1,4}, {3,6}, {4,5}, {5,8}, {6,7}, {7,8}]

if userInput.upper() == 'EAST':
    newUserPoint = userPoint + 1
elif userInput.upper() == 'WEST':
    newUserPoint = userPoint - 1
elif userInput.upper() == 'SOUTH':
    newUserPoint = userPoint + 3
elif userInput.upper() == 'NORTH':
    newUserPoint = userPoint - 3
else:
    newUserPoint = None

movementIsValid = {userPoint, newUserPoint} in connections
if movementIsValid:
    userPoint = newUserPoint
else:
    print('Please input a valid direction.\n')
</code></pre>

<p>To learn about how lists and sets are used, you can check out the tutorial (<a href="https://docs.python.org/3.5/tutorial/introduction.html#lists" rel="nofollow">lists</a>, <a href="https://docs.python.org/3.5/tutorial/datastructures.html#sets" rel="nofollow">sets</a>).  In the code above, we calculate the new point and then check if there is a connection from the old point to the new point.</p>

<p>There is still some repetition that we can get rid of when we calculate the new point.</p>

<pre><code>if userInput.upper() == 'QUIT':
    break

directions = {'EAST': 1, 'WEST': -1, 'SOUTH': 3, 'NORTH': -3}
connections = [{0,1}, {1,2}, {1,4}, {3,6}, {4,5}, {5,8}, {6,7}, {7,8}]

if userInput.upper() in directions:
    userDirection = directions[userInput.upper()]
    newUserPoint = userPoint + userDirection
else:
    newUserPoint = None

movementIsValid = {userPoint, newUserPoint} in connections
if movementIsValid:
    userPoint = newUserPoint
else:
    print('Please input a valid direction.\n')
</code></pre>

<p>In this solution, we use a <a href="https://docs.python.org/3.5/tutorial/datastructures.html#dictionaries" rel="nofollow">dictionary</a> for the directions.</p>

<p>I've used a lot of concepts that might be new for you, you can read about them in the links I've given, and if you get stuck anywhere you can ask a new question here on StackOverflow.</p>

python python-3.x