Two Powershells showing running the scan function and running nosetests
在更新的学习python 3中,我很难让ex48代码工作。在本练习中,我们给出了测试脚本(我将6个案例缩减为1,因为它们都返回了相同的错误)并告诉他们编写一个lexicon.py文件,使得测试(通过nosetests)不会返回任何错误:
from nose.tools import *
from ex48 import lexicon
def test_directions():
assert_equal(lexicon.scan("north"), [('direction', 'north')])
result = lexicon.scan("north south east")
assert_equal(result, [('direction', 'north'),
('direction', 'south'),
('direction', 'east')])
我的代码lexicon.py脚本如下:
directions = ('north', 'south', 'east')
verbs = ('go', 'kill', 'eat')
stops = ('the', 'in', 'of')
nouns = ('bear', 'princess')
def get_tuple(word):
lowercased = word.lower()
if lowercased in directions:
return('direction', lowercased)
elif lowercased in verbs:
return('verb', lowercased)
elif lowercased in stops:
return('stop', lowercased)
elif lowercased in nouns:
return('noun', lowercased)
elif lowercased.isdigit():
return('number', int(lowercased))
else:
return('error', word)
def scan(sentence):
pairs = []
text = str(sentence)
words = text.split()
for item in words:
try:
tup = get_tuple(item)
pairs.append(tup)
except:
pass
print(pairs)
这是我的错误代码:
File , line 6, in test_directions
assert_equal(lexicon.scan("north"), [('direction', 'north')])
AssertionError: None != [('direction', 'north')]
-------------------- >> begin captured stdout << ---------------------
[('direction', 'north')]
当我直接在powershell中运行python中的程序时,扫描函数似乎总是得到正确的输出。如果我输入任何字符串,如“北方记得”,我会得到以下输出:
[('stop', 'the'), ('direction', 'north'), ('error', 'remembers)]
有人能看出为什么nosetests会抛出这个错误吗?
编辑 - 删除'return'命令时出现问题,但基础错误不是返回打印。 Nosetests仍然具有与以下代码相同的错误:
我现在修改了代码,如下所示:
def scan(sentence):
pairs = []
text = str(sentence)
words = text.split()
for item in words:
tup = get_tuple(item)
pairs.append(tup)
return pairs