まず、それは私がテストを必要とするコードです:Pythonの:AssertionErrorが
class ParserError(Exception):
pass
class Sentence(object):
def __init__(self, subject, verb, object):
self.subject = subject [1]
self.verb = verb[1]
self.object = object[1]
def peek(word_list):
if word_list:
word = word_list[0]
return word[0]
else:
return None
def match(word_list, expecting):
if word_list:
word = word_list.pop(0)
if word[0] == expecting:
return word
else:
return None
else:
return None
def skip (word_list, word_type):
while peek(word_list) == word_type:
match(word_list, word_type)
def parse_verb(word_list):
skip(word_list, 'stop')
if peek(word_list) == 'verb':
return match(word_list, 'verb')
else:
raise ParserError("Expected a verb next.")
def parse_object(word_list):
skip(word_list,'stop')
next = peek(word_list)
if next == 'noun':
return match(word_list, 'noun')
if next == 'direction':
return match(word_list, 'direction')
else:
raise ParserError('Expected a noun or direction next.')
def parse_subject(word_list, subj):
verb = parse_verb(word_list)
obj = parse_object(word_list)
return Sentence(subj, verb, obj) # 执行 class Sentence
def parse_sentence(word_list):
skip(word_list, 'stop')
start = peek(word_list)
if start == 'noun':
subj = match(word_list, 'noun')
return parse_sentence(word_list, subj)
elif start == 'verb':
# assume the subject is the player then
return parse_subject(word_list, ('noun', 'player'))
else:
raise ParserError("Must start with subject, object, or verb not: %s" % start)
私が入力されたリストは、[( '動詞'、 '味')、( '方向'、 '良い')です、 ( 'noun'、 'pizza')] このリストの最初の値は( '動詞'、 '味')で、そのword_typeは '動詞'で、私のコーディングで与えたword_typeに合致しますが、エラーが発生しました AssertionError:None!= '動詞' 私はそれが不合理だと思います。
def test_skip():
skipA = skip([('verb','taste'),('direction','good'),('noun','pizza')], 'verb')
assert_equal(skipA, 'verb')
# assert_equal(skipA, None)
が、「言葉[0] ==期待している場合: 戻り単語を」私が書いた試合で私はあなたがmatch' 'から戻っていますが、' – Electro01
を返すべきだと思いますしかし、私はまだちょっと混乱している私は関数の最後の部分をスキップと思う、それは一致を呼び出すので、それはvauleを返すかもしれない – alfasin
をskip'から返されないことがElectro01 @単語 – Electro01