在学习Python困难之路上遇到一些麻烦48

时间:2011-07-30 18:27:33

标签: python python-2.7

在本节中,我们给出了一系列单元测试,并且需要创建一个使测试通过的函数。这是测试:

from nose.tools import *
from testing import *


def test_numbers():
    assert_equal(scan("1234"), [('number', 1234)])
    result = scan("3 91234")
    assert_equal(result, [('number', 3),
                      ('number', 91234)])

测试还有其他方面,但这是唯一没有通过的方面。这是我写的:

def convert_number(s):
    try: 
        return int(s)
    except ValueError:
        return None

def scan(s):

    direction_words= ("north", "south", "east", "west", "down", "up", "left", "right", "back")
    verbs= ("go", "stop", "kill", "eat")
    stop_words= ("the", "in", "of", "from", "at", "it")
    nouns= ("door", "bear", "princess", "cabinet")        
    numbers= s.split()
    i=0
    j=0
    g=0
    m=0
    a=0


    while a< len(numbers):
        if  type(convert_number(numbers[a])) == int:
            return [('number', int(x) ) for x in s.split()]
        else:
            a += 1


    while i < 9:
        if direction_words[i] in s.split():
            return [('direction', x ) for x in s.split()]
        else:
            i+=1              

    while j < 4:
        if verbs[j] in s.split():
            return [('verb', x ) for x in s.split()]
        else:
            j+=1

    while g < 6:
         if stop_words[g] in s.split():
             return [('stop', x ) for x in s.split()]
         else:
            g+=1

    while m < 4:
         if nouns[m] in s.split():
             return [('noun', x ) for x in s.split()] 
        else:
            m+=1

    else:
        return 

2 个答案:

答案 0 :(得分:1)

只需在@staticmethod上方添加def scan(s):即可使其正常运行。

答案 1 :(得分:0)

我怀疑你当然想要def scan(self, s):而不是def scan(s):