2016-06-20 7 views
1

はここですでに書かれたテストのために私のコードです私はこのコードを実行します。Pythonのテストエラーが

ERROR: Failure: SyntaxError (invalid syntax (lexicon.py, line 19)) 
---------------------------------------------------------------------- 
Traceback (most recent call last): 
    File "/usr/lib/python2.7/site-packages/nose/loader.py", line 418, in loadTestsFromName 
    addr.filename, addr.module) 
    File "/usr/lib/python2.7/site-packages/nose/importer.py", line 47, in importFromPath 
    return self.importFromDir(dir_path, fqname) 
    File "/usr/lib/python2.7/site-packages/nose/importer.py", line 94, in importFromDir 
    mod = load_module(part_fqname, fh, filename, desc) 
    File "/home/george/Documents/git-docs/pythonlearning/ex48/tests/lexicon_tests.py", line 2, in <module> 
    from LEXICON import lexicon 
    File "/home/george/Documents/git-docs/pythonlearning/ex48/LEXICON/lexicon.py", line 19 
    for i in words: 
       ^
SyntaxError: invalid syntax 

---------------------------------------------------------------------- 
Ran 1 test in 0.004s 

FAILED (errors=1) 

質問: 文の中のエラーとは何ですか?

+1

欠落を ']'上記の行インチまた、 'return'は間違ってインデントされています。また、(タプルの代わりに)2つの引数を渡しています。また、 'direction in words'も奇妙に見えます。' words'はリストです。 –

+0

あなたが望む正確なものがわからないが、現在は ']'が現在のエラーの理由です。 –

+0

tobiasありがとう、 ']'があります。ユーザーが複数の引数を指定した場合は、r変数に複数のタプルを追加します。私がそこに持っているものはこの ''( 'ディレクトリ'、 '北南')] 'を与えるだけですが、私が望むものは' '( '方向'、 '北')、( '方向'、 '南')]' –

答えて

0

3. 変更し、それが当面の問題が欠落している]ですが、そのシンプルなタイプミスを越えて、より存在しているラインで「]」閉じ括弧を追加するために逃した:

    は、
  • words in directionsはあまり意味がないようですが、wordsはまだ1つの要素しか持っていなくてもまだリストになっています。あなたはwords[0]を使用することができますが、これは場合wordsに失敗することは常に最後の要素だけ
  • が含まれています。すなわち、リストの初期化r = []はあなたがappend(("direction", i))を意味し、appendに二つの引数を渡すことはできません、ループである
  • 空であります
  • return rはまた、あなたは、単一のリストの内包にその全体の機能を有効にすることができますインデント

の間違ったレベルにある:

def scan(self, *words): 
    return [("direction", w) for w in words if w in directions] 

例:

>>> directions = ["north", "south", "east", "west"] 
>>> lex = lexicon() 
>>> lex.scan() 
[] 
>>> lex.scan("north") 
[('direction', 'north')] 
>>> lex.scan("north", "south", "west", "not a direction") 
[('direction', 'north'), ('direction', 'south'), ('direction', 'west')] 

注、しかしながら、*wordsは言葉は、例えば、個別関数に渡されることを前提としていscan("i", "went", "north")のように。代わりに、あなたはscan("i went north")として、それを呼び出したい場合は、あなたがsplitに関数内で単語に文を持っている:

def scan(self, sentence): 
    words = sentence.split() 
    return [("direction", w) for w in words if w in directions] 
+0

Tobias私が 'scan( '私は北に行きました')を渡すと、空のリストが返されます! –

+0

''私は北に行きました ""は単一の文字列です。つまり、 '* words'は' ["i" north "]'です。 '' words''を '[" "i"、 "went"、 "north"] 'にしたい場合は、scan("北 "の" .split()) 'またはメソッド内の' split (そして、パラメータ '* words'を' sentence'などに変更してこれをクリアします)。 –

+0

あなたはTobias、ありがとう、それを殺した。 –

0

あなたは

def scan(self, *words): # '*words' lets you give a variable number of arguments to a function 
    if len(words) <= 1 and words in direction: 
     r = [('direction', words)] 
    else: 
     for i in words: 
      r = [] 
      r.append('direction', i)    
    return r 
+0

これにはまだ複数のエラーがあります。 –

関連する問題