正規表現を練習するために、Zork に似た非常に単純なテキスト ベースのゲームを作成しようとしています。ただし、正規表現を使用してコードを機能させることができないようです。
動き.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 サンプルラン
>>> import movement
>>> moves = movement.userMove()
Which direction do you want to move?Lets walk East
['Lets', 'walk', 'East']
['East']
North
サンプルランの場合
>>> import movement
>>> moves = movement.userMove()
Which direction do you want to move?I`ll run North
['I`ll', 'run', 'North']
['North']
West
for
ループが常にmovement
北に設定される場合。if
の代わりにステートメントを使用elif
すると、西に設定されます。userInput
代わりに正規表現を使用するtextArray
と、メソッドがmovement
null のままになります。
編集
コードをさらにテストして変更した後、正規表現は問題なく、if
ステートメントまたはfor
ループのバグであると確信しています。