0

我有奇怪的情况。我有代码(来自 Z.Shaw Learn the python the hard way):

WORD_TYPES={
   'verb' : ['go', 'kill', 'eat'],
   'direction' : ['north', 'south', 'east', 'west'],
   'noun' : ['bear', 'princess'],
   'stop' : ['the','in','of'],
   'adjective': ['beautiful','cool','nice']
   }

VOCABULARY={word: word_type for word_type, words in WORD_TYPES.items() for word in words}

def scan(sentence):
    tokens=[]
    for word in sentence.lower().split():
        try:
            word_type=VOCABULARY[word]
        except KeyError:
            try:
                value=int(word)
            except ValueError:
                tokens.append(('error',word))
            else:
                tokens.append(('int',word))

    else:
        tokens.append((word_type,word))

    for i in range(0,len(tokens)):
         if tokens[i][0] == "error":
            del tokens[i]
return tokens

当我用“美丽”这个词尝试它时 - 它按预期返回

[('adjective', u'beautiful')]

但是鼻子测试给出了不可预测的结果,我看不出原因:

from nose.tools import *
from Ex_48 import lexicon
def test_upper_lower_letters():
    assert_equal(lexicon.scan("Beautiful"),[('adjective','beautiful')])
    assert_equal(lexicon.scan("BEauTiful"),[('adjective','beautiful')])

这就是我得到的:

C:\Python27\projects\skeleton\tests>nosetests tests_Ex_48
.......F
======================================================================
FAIL: tests_Ex_48.test_upper_lower_letters
----------------------------------------------------------------------
Traceback (most recent call last):
  File "C:\Python27\lib\site-packages\nose-1.3.0-py2.7.egg\nose\case.py", line 197, in runTest
    self.test(*self.arg)
  File "C:\Python27\projects\skeleton\tests\tests_Ex_48.py", line 58, in test_upper_lower_letters
    assert_equal(lexicon.scan("Beautiful"),[('adjective','beautiful')])
AssertionError: Lists differ: [('error', 'Beautiful')] != [('adjective', 'beautiful')]

First differing element 0:
('error', 'Beautiful')
('adjective', 'beautiful')

- [('error', 'Beautiful')]
+ [('adjective', 'beautiful')]

----------------------------------------------------------------------
Ran 8 tests in 0.002s

FAILED (failures=1)

请帮忙

4

0 回答 0