【发布时间】:2017-07-27 21:36:56
【问题描述】:
我无法正确测试下面的代码。 test_errors() 函数无法正常工作,但我觉得我的代码设置正确。第 25 行是我认为可行的,但我没有任何运气。
第 25 行:
elif i not in direction or verb or stop or noun:
scan_result = scan_result + [('error', i)]
完整代码:
direction = ('north', 'south', 'east', 'west', 'up', 'down', 'left', 'right', 'back')
verb = ('go', 'stop', 'kill', 'eat')
stop = ('the', 'in', 'of', 'from', 'at', 'it')
noun = ('door', 'bear', 'princess', 'cabinet')
lexicon = {direction: 'direction',
verb: 'verb',
stop: 'stop',
noun: 'noun',
}
def scan(user_input):
words = user_input.split()
scan_result = []
try:
for i in words:
if i.isdigit():
scan_result = scan_result + [('number', int(i))]
elif i.lower() in direction or verb or stop or noun:
for j in lexicon:
for k in j:
if i.lower() == k:
scan_result = scan_result + [(lexicon[j], i)]
elif i not in direction or verb or stop or noun:
scan_result = scan_result + [('error', i)]
return scan_result
except ValueError:
return None
test_error 函数:
def test_errors():
assert_equal(lexicon.scan("ASDFADFASDF"), [('error', 'ASDFADFASDF')])
result = lexicon.scan("bear IAS princess")
assert_equal(result, [('noun', 'bear')
('error', 'IAS'),
('noun', 'princess')])
【问题讨论】:
-
请:make a relevant title。这不是 LPTHW 支持论坛。
-
我通过执行
print scan("go in the door")运行了您的代码,它似乎返回了在您创建时已解析的元组列表。当我提供错误的输入(不在单词列表中的任何内容)时,这些单词不会包含在结果中。即“熊IAS公主”=> [('名词','熊'),('名词','公主')] -
Juanpa,以后一定会尝试的。真的不知道如何描述问题。 Sunny,是的,这就是我遇到的问题。如果有错误,它只是默默地通过扫描
标签: python python-2.7 error-handling automated-tests