gpt4 book ai didi

python - ex48 艰难地学习 Python

转载 作者:太空宇宙 更新时间:2023-11-03 14:28:14 25 4
gpt4 key购买 nike

<分区>

direction = ['north', 'south', 'east', 'west', 'down', 'up', 'left', 'right', 'back']
verbs = ['go', 'stop', 'kill', 'eat']
stop = ['the', 'in', 'of', 'from', 'at', 'it']
nouns = ['door', 'bear', 'princess', 'cabinet']
numbers = [i for i in range(10)]


class lexicon(object):

def scan(self, sentence):
self.sentence = sentence
self.words = sentence.split()
for word in self.words:
if word is direction:
word = ('direction','%s' % word)

return word

http://learnpythonthehardway.org/book/ex48.html是我正在做的,我不知道为什么我的程序没有通过测试。当我运行 nosetests 时,出现此错误。

ERROR: tests.ex48_tests.test_directions
----------------------------------------------------------------------
Traceback (most recent call last):
File "/Users/tplaw/Public/projects/installs/venv/lib/python2.7/site-packages/nose/case.py", line 197, in runTest
self.test(*self.arg)
File "/Users/tplaw/Public/projects/ex48/tests/ex48_tests.py", line 6, in test_directions
assert_equal(lexicon.scan("north"), [('direction', 'north')])
TypeError: unbound method scan() must be called with lexicon instance as first argument (got str instance instead)

----------------------------------------------------------------------
Ran 1 test in 0.005s

FAILED (errors=1)

并且我只将测试的第一个 aprt 放在我的测试目录中。是这样的:

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')])

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com