Python 2.7 & Regular Expressions: False if statement returns
Question: To practice regular expressions, I'm trying to create a very simple text based
game similar to Zork. However I can't seem to have the code work using regular
expressions.

**Movement.py**

    
    
    import re
    
    def userMove():
        userInput = raw_input('Which direction do you want to move?')
        textArray = userInput.split()
        analyse = [v for v in textArray if re.search('north|east|south|west|^[NESW]', v, re.IGNORECASE)]
    
       print textArray
       print analyse
    
       movement = 'null'
       for string in analyse:
           if string is 'North' or 'n':
              movement = 'North'
           elif string is 'East'or'e':
              movement = 'East'
           elif string is 'South'or's':
              movement = 'South'
           elif string is 'West'or'w':
              movement = 'West'
    
    print movement
    

**if/elif Sample Run**

    
    
    >>> import movement
    >>> moves = movement.userMove()
    Which direction do you want to move?Lets walk East
    ['Lets', 'walk', 'East']
    ['East']
    North
    

**if Sample Run**

    
    
    >>> import movement
    >>> moves = movement.userMove()
    Which direction do you want to move?I`ll run North
    ['I`ll', 'run', 'North']
    ['North']
    West
    

If the `for` loop will constantly set `movement` to North; and using `if`
statements instead of `elif` will set it to West. Making the regex use
`userInput` in place of `textArray` cause the method to keep `movement` as
null.

**_Edit_** After further testing and changing the code, I'm sure the regex is
fine and it's a bug with the `if` statements or the `for` loop.


Answer: Your problem is with these `if` statements:

    
    
    if string is 'North' or 'n':
        movement = 'North'
    elif string is 'East'or'e':
        movement = 'East'
    elif string is 'South'or's':
        movement = 'South'
    etc...
    

They don't work quite how you're expecting. First, you shouldn't compare
strings with `is` \- you should use `==`. Secondly, the statement is evaluated
more like:

    
    
    if (string is 'North') or 'n':
        movement = 'North'
    

So, `'n'` is always `True` \- meaning your `movement` variable is always set
to `North`.

Try this instead:

    
    
    if string in ('North', 'n'):
        etc...
    

