OKが私のコードです:pythonの辞書の繰り返しの使用エラー
# create variables
direction = ('north', 'south', 'east', 'west', 'down', 'up', 'left', 'right', 'back')
verb = ('go', 'stop', 'kill', 'eat')
stop = ('the', 'in', 'of', 'from', 'at', 'it')
noun = ('door', 'bear', 'princess', 'cabinet')
number = ('0', '1', '2', '3', '4', '5', '6', '7', '8', '9')
def scan(word, *words): # '*words' lets you give a variable number of arguments to a function
words = word.split()
src = { direction : 'direction',
verb : 'verb',
stop : 'stop',
noun : 'noun',
number : 'number'
}
for k, v in src.items():
if words in k:
m = src.get(k)
print [(m, w) for w in words]
scan("I was going north and south")
問題:
- 私が1かどうかを確認するために
src
を通じて言葉を実行した後variable m
にdic(src)
の値を代入しようとしていますwords
の内容は、src
に見つけることができます。
マイエラー:
Traceback (most recent call last):
File "koko2.py", line 26, in <module>
scan("I was going north and south")
File "koko2.py", line 24, in scan
print [(m, w) for w in words if w in direction or verb or stop or noun or number]
UnboundLocalError: local variable 'm' referenced before assignment
'for'ループは、' k in words in k 'があれば 'm' ** only **を定義します。 'for'の中に' print'を追加して、何が起きているのか、それがあなたの期待に合っているかを見てみましょう。 – Bakuriu
「動詞または停止または名詞または番号」とは何としますか? –
Padraic実際にコード内に重複しているプロセスがあるので、私はそれを削除しました。 –