2011-04-11 26 views
1

我想學Python,我遵循的指南是要求我寫一個簡單的'遊戲',利用元組,列表和類。nosetests,python

當運行 'nosetests' 命令,我收到以下錯誤:

E. 
====================================================================== ERROR: 
tests.LEXICON_tests.test_directions 
---------------------------------------------------------------------- 
Traceback (most recent call last): 
File "/Library/Python/2.6/site-packages/nose/case.py", 
line 187, in runTest 
    self.test(*self.arg) 
File "/Users/VB/Documents/svn/Programming/python/projects/lexicon/tests/LEXICON_tests.py", 
line 6, in test_directions 
    assert_equal(lexicon.scan("north"), 
[('directions', 'north')]) TypeError: 
unbound method scan() must be called 
with lexicon instance as first 
argument (got str instance instead) 

---------------------------------------------------------------------- Ran 2 tests in 
0.011s 

FAILED (errors=1) VB MP > ./lexicon.py 

> north [(), ('directions', 'north')] VB MP > ./lexicon.py 
> north south east [[[(), ('directions', 'north')], 
('directions', 'south')], 
('directions', 'east')] VB MP 

主文件:

#!/usr/bin/env python 
# encoding: utf-8 

import sys 
import os 
from LEXICON.game import lexicon 


def main(): 
    stuff = raw_input('> ') 

    lex = lexicon (stuff) 

    split_array = lex.scan(stuff) 
    print split_array 

    #me = lex.tockens(split_array) 
    #print me 

if __name__ == '__main__': 
    main() 

class 
#!/usr/bin/env python 
# encoding: utf-8 
""" 

import sys 
import os 

class lexicon (object): 
    def __init__(self,data): 
     self.direction = data 
     #self.words = data.split() 

    def scan(self,data): 
     split_data = data.split() 
     lex = lexicon (data) 

     tocken = lex.tockens(split_data) 


     return tocken 

    def tockens(self,splitdata): 

     sentence = splitdata 
     verbs = ['go','swim','open','punch','fly','stop','kill','eat'] 
     objekts = ['door','bear','face','princess','cabinet'] 
     directions = ['north','south','east','west','down','up','left','right','back'] 
     stopwords = ['the','in','of','from','at','it'] 


     # setep 1, open the array 

     bit =() 
     byte =() 
     for x in sentence: 
      # step 2, match against verbs 
      for v in verbs: 
       try: 
        if (x == v): 
         bit = ('verb',x) 
         byte = [byte, bit]    
       except ValueError: 
        print "Ooops!" 
      for o in objekts: 
       try: 
        if (x == o): 
         bit = ('objekt',x) 
         byte = [byte, bit] 
       except ValueError: 
        print "Ooops!" 
      for d in directions: 
       try: 
        if (x == d): 
         bit = ('directions',x) 
         byte = [byte, bit] 
       except ValueError: 
        print "Ooops!" 
      for s in stopwords: 
       try: 
        if (x == s): 
         bit = ('stopwords',x) 
         byte = [byte, bit] 
       except ValueError: 
        print "Ooops!" 

     return byte 

測試

from nose.tools import * 
#import LEXICON 
from LEXICON.game import lexicon 

def test_directions(): 
    assert_equal(lexicon.scan("north"), [('directions', 'north')]) 
    result = lexicon.scan("north south east") 
    assert_equal(result, [('directions', 'north'), 
          ('directions', 'south'), 
          ('directions', 'east')]) 

謝謝!

+1

請把你的代碼和錯誤在你的問題。 – GWW 2011-04-11 21:11:11

+0

學習Python的艱辛之路 - 很好地完成 – Stedy 2011-04-11 21:13:30

+0

把代碼放在這裏,而不是放在pastebin上。 – pajton 2011-04-11 21:17:57

回答

2

在python中調用方法的方式是將它所調用的對象作爲第一個參數傳遞,並且提供的參數每個都被向下推送1.​​當您調用它的靜態方法(lexicon.scan)時實例方法(lex.scan)未提供此第一個參數。

lexicon.scan要求第一個參數是「詞典」類型對象,因此您可能想要在測試中做的是創建一個詞典對象(lex = lexicon(stuff))並從該對象調用掃描(lex.scan("north"))。因爲它呼叫scan("north"),而您想要呼叫爲scan(lex, "north")

+0

非常感謝!這正是我缺少的部分,需要幫助! 之間有什麼 '結果的差異= lexicon.scan( 「北東南」)' 和 '=法詞庫(東西) split_array = lex.scan(東西)' – Cmag 2011-04-12 15:35:22

+0

測試代碼是在提供在文檔中的例子,這可能是調用一個類的正確方式,而不是我正在做的方式......你會推薦哪一種?謝謝! – Cmag 2011-04-12 15:39:01

+0

你說什麼叫一個班?請詳細說明。 – 2011-04-12 18:17:18