我知道已經有很多人問這個問題看起來像這樣,但我真的找不到一個適合我的解決方案。'模塊'的對象沒有屬性'scan'.narn python困難的方式ex48
在ex48。我寫的 「lexicon.py」 和nosetests它。但是它報告錯誤:
Traceback (most recent call last):
File "c:\python27\lib\site-packages\nose\case.py", line 197, in runTest
self.test(*self.arg)
File "D:\pyhomework\ex48\skeleton\tests\lexicon_tests.py", line 31, in test_errors
assert_equal(Lexicon.scan("ASDFASDF"),[('errors','ASDFASDF')])
AttributeError的: '模塊' 對象有沒有屬性 '掃描'
這是lexicon.py:
class Lexicon(object):
def __init__(self):
self.dic = {'direction':('north','south','east'),
'verb':('go','kill','eat'),
'stop':('the','in','of'),
'noun':('bear','princess'),
'number':('1234','3','91234'),
'error':('ASDFASDF','IAS')}
def scan(self,words):
self.word = words.split()
self.result = []
for item in self.word:
for key,value in self.dic.items():
if item in value:
self.result.append((key,item))
return self.result
這是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_verbs():
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("ASDFASDF"),[('errors','ASDFASDF')])
result = Lexicon.scan("bear IAS princess")
assert_equal(result,[('noun','bear')('error','IAS')('noun','princess')])
這是骨架:
-bin
-docs
-ex48 (-__init__.py -Lexicon.py)
-tests (-__init__.py -lexicon_tests.py)
-setup
我已經嘗試了幾種方法,但仍然得到相同的錯誤。 感謝您的任何建議。