有关这些代码用途的更多详细信息,请参阅LearningPythonTheHardWay exercise48。我还是个新手,我知道还有很多地方需要改进,或者有更好的方法去做。
我在这里遇到的主要问题是nosetest的结果,代码在lexicon.py之后附加在下面。
简而言之,我有1个错误和5个失败。
错误:
NameError: global name 'ASDFADFASDF' is not defined失败:
AssertionError: None != [('direction', 'north')]
AssertionError: None != [('verb', 'go')]
AssertionError: None != [('stop', 'the')]
AssertionError: None != [('noun', 'bear')]
AssertionError: None != [('number', 1234)]其中结果是无,而不是右侧的答案。对于错误,我在代码中得到了类似的结果,只是与test_error进行了比较,在那里我有
[('error', 'ASDFADFASDF')]测试结果是
[('error', ASDFADFASDF)]至于失败,我得到的典型失败消息是:
File "C:\xxx\xxxxxx\learningpython\projectex48\skeleton\tests\lexicon_tests.py", line 13, in test_verb
assert_equal(lexicon.scan("go"),[('verb', 'go')])
AssertionError: None != [('verb', 'go')]
-------------------- >> begin captured stdout << ---------------------
[('verb', 'go')]
--------------------- >> end captured stdout << ----------------------我不知道为什么它只运行一个元素,比如"go kill eat“python只运行"go”。我仍然在努力弄清楚其中的逻辑以及如何修复它。谁能帮我找出为什么我没有通过5个测试并有1个错误的问题。
我花了几周的时间为这个练习编写我自己版本的扫描函数,这是lexicon.py的代码:
direction = [('direction', 'north'),
('direction', 'south'),
('direction', 'east'),
('direction', 'west'),
('direction', 'up'),
('direction', 'down'),
('direction', 'left'),
('direction', 'right'),
('direction', 'back')
]
verbs = [('verb', 'go'),
('verb', 'stop'),
('verb', 'kill'),
('verb', 'eat')
]
stop_words = [('stop', 'the'),
('stop', 'in'),
('stop', 'of'),
('stop', 'from'),
('stop', 'at'),
('stop', 'it')
]
nouns = [('noun', 'door'),
('noun', 'bear'),
('noun', 'princess'),
('noun', 'cabinet')
]
library = tuple(nouns + stop_words + verbs + direction)
def convert_number(x):
try:
return int(x)
except ValueError:
return None
def scan(input):
element = input.split()
data = library
i = 0
z = 0
output = []
temp = True
while (i == 0) or (not (element[(i-1)] == element[-1])):
try:
j = 0
while (j == 0) or (not data[(j-1)][1] == data [-1][1]):
matching = data[j][1]
if (matching == element[i]):
output.append(data[j])
j += 1
z += 1
temp = False
else:
while (data[j][1] == data [-1][1]) and (temp == True):
convert = convert_number(element[i])
a = tuple(['number', convert])
b = tuple(['error', element[i]])
if convert == a[1] and not(convert == None):
output.append(a)
temp = False
else:
output.append(b)
temp = False
j += 1
i += 1
temp = True
except ValueError:
return None
else:
pass
print output上面是我用来测试的简化版本,其概念是搜索和收集/添加与输入对应的数据,并将其作为元组保存并附加到输出中,最后将它们一起打印出来。
下面是我直接从书中复制的lexicon_tests.py代码:
from nose.tools import *
from ex48 import lexicon
def test_direction():
assert_equal(lexicon.scan("north"), [('direction', 'north')])
result = lexicon.scan("north south east")
assert_equal(result, [('direction', 'north'),
('direction', 'south'),
('direction', 'east')])
def test_verb():
assert_equal(lexicon.scan("go"),[('verb', 'go')])
result = lexicon.scan("go kill eat")
assert_equal(result, [('verb', 'go'),
('verb', 'kill'),
('verb', 'eat')])
def test_stops():
assert_equal(lexicon.scan("the"), [('stop', 'the')])
result = lexicon.scan("the in of")
assert_equal(result, [('stop', 'the'),
('stop', 'in'),
('stop', 'of')])
def test_nouns():
assert_equal(lexicon.scan("bear"), [('noun', 'bear')])
result = lexicon.scan("bear princess")
assert_equal(result, [('noun', 'bear'),
('noun', 'princess')])
def test_numbers():
assert_equal(lexicon.scan("1234"), [('number', 1234)])
result = lexicon.scan("3 91234")
assert_equal(result, [('number', 3),
('number', 91234)])
def test_errors():
assert_equal(lexicon.scan("ASDFADFASDF"), [('error', ASDFADFASDF)]) #refer to Update#1
result = lexicon.scan("bear IAS princess")
assert_equal(result, [('noun', 'bear'),
('error', 'IAS'),
('noun', 'princess')])这是我保存文件的位置:
$ mkdir projectsex48
$ cd projectsex48/
$ mkdir skeleton
$ cd skeleton
$ mkdir bin
$ mkdir ex48 #(lexicon.py)
$ mkdir tests #(lexicon_tests.py)
$ mkdir doc**更新#1:**针对错误
NameError: global name 'ASDFADFASDF' is not defined这是因为ASDFADFASDF的lexicon_tests.py中缺少括号'‘,如下所示:
assert_equal(lexicon.scan("ASDFADFASDF"), [('error', 'ASDFADFASDF')])现在我有6次失败,函数返回None。
AssertionError: None != [('direction', 'north')]
AssertionError: None != [('verb', 'go')]
AssertionError: None != [('stop', 'the')]
AssertionError: None != [('noun', 'bear')]
AssertionError: None != [('number', 1234)]
AssertionError: None != [('error', 'ASDFADFASDF')]发布于 2018-12-23 00:22:32
是的!!我终于解决了这个问题。
我犯了一个错误,用以下命令结束了扫描函数
print output我改正了它
return output所以最终修正后的代码是:
direction = [('direction', 'north'),
('direction', 'south'),
('direction', 'east'),
('direction', 'west'),
('direction', 'down'),
('direction', 'left'),
('direction', 'right'),
('direction', 'back')
]
verbs = [('verb', 'go'),
('verb', 'stop'),
('verb', 'kill'),
('verb', 'eat')
]
stop_words = [('stop', 'the'),
('stop', 'in'),
('stop', 'of'),
('stop', 'from'),
('stop', 'at'),
('stop', 'it')
]
nouns = [('noun', 'door'),
('noun', 'bear'),
('noun', 'princess'),
('noun', 'cabinet')
]
library = tuple(nouns + stop_words + verbs + direction)
def convert_number(x):
try:
return int(x)
except ValueError:
return None
def scan(input):
#include uppercase input for searching. (Study Drills no.3)
lowercase = input.lower()
#element is what i want to search.
element = lowercase.split()
#orielement is the original input which have uppercase, for 'error' type
orielement = input.split()
#library is tuple of the word types from above. You can replace with your data source.
data = library
#i is used to evaluate the position of element
i = 0
#z is used to indicate the position of output, which is the data that match what i search, equals to "i".
z = 0
#create a place to store my output.
output = []
#temp is just a on/off switch. Turn off the switch when i get any match for that particular input.
temp = True
#creating a condition which evaluates the total search needed to be done and follows the sequence by +1.
while not(i == len(element)):
try:
#j is used to position the word in the library, eg 'door', 'bear', 'go', etc which exclude the word type.
j = 0
while not (j == len(data)):
#data[j][1] all the single word in library
matching = data[j][1]
#when the word match, it will save the match into the output.
if (matching == element[i]):
output.append(data[j])
#print output[z]
j += 1
z += 1
#to switch off the search for else: below and go to next input search. Otherwise they would be considerd 'error'
temp = False
#else is everything that is not in the library.
else:
while (data[j][1] == data [-1][1]) and (temp == True):
#refer to convert_number, to test if the input is a number, here i use orielement which includes uppercase
convert = convert_number(orielement[i])
#a is used to save number only.
a = tuple(['number', convert])
#b is to save everything
b = tuple(['error', orielement[i]])
#conver is number a[1] is the access the number inside, if it returns None from number then it wont append.
if convert == a[1] and not(convert == None):
output.append(a)
temp = False
else:
output.append(b)
#keep the switch off to escape the while loop!
temp = False
#searching in next data
j += 1
#next word of input
i += 1
temp = True
except ValueError:
return output
else:
pass
return outputoutput是我用来存储搜索结果的元组。
https://stackoverflow.com/questions/53881802
复制相似问题