找回密码
 立即注册

扫一扫,访问微社区

QQ登录

只需一步,快速开始

查看: 5563|回复: 2

[已回复] 在看 笨办法学Python , 卡在 48 节两天了

1

主题

1

帖子

0

积分

贫民

积分
0
pythoon 发表于 2013-11-19 15:46:11 | 显示全部楼层 |阅读模式
在看 笨办法学Python , 卡在 48 节两天了
运行 nosetests 一直出错 , 帮个忙 告诉我这是什么原因??
2013-11-19 15.38.25.png

下面这个是 ex48/ex48.py

[code brush:python;toolbar:false;]class lexicon(object):
  
    direction   =   ['north', 'south'   , 'east'    , 'west', 'down', 'up', 'left', 'right', 'back']
    verb        =   ['go'   , 'stop'    , 'kill'    , 'eat']
    stop        =   ['the'  , 'in'      , 'of'      , 'from', 'at'  , 'it']
    noun        =   ['door' , 'bear'    , 'princess', 'cabinet']   
  
    def __init__(self):
        self.finalList = []
  
    @staticmethod
    def isnum(s):
        try:
            return int(s)
        except ValueError:
            return None
  
    def scan(self , sentence):
        words = sentence.split(" ")
  
        for i in words:
            if self.isnum(i):
                newGroup = ('number' , int(i))
                self.finalList.append(newGroup)
            else:
                flag = 0
  
                for ii in lexicon.direction:
                    if ii == i:
                        newGroup = ('direction', ii)
                        self.finalList.append(newGroup)
                        flag = 1
  
                for ii in lexicon.verb:
                    if ii == i and flag != 1:
                        newGroup = ('verb', ii)
                        self.finalList.append(newGroup)
                        flag = 1
  
                for ii in lexicon.stop:
                    if ii == i and flag != 1:
                        newGroup = ('stop', ii)
                        self.finalList.append(newGroup)
                        flag = 1
  
                for ii in lexicon.noun:
                    if ii == i and flag != 1:
                        newGroup = ('noun', ii)
                        self.finalList.append(newGroup)
                        flag = 1
  
                if flag != 1:
                    newGroup = ('error', i)
                    self.finalList.append(newGroup)
  
        return self.finalList[/code]这个可以运行
2013-11-19 15.40.08.png

下面这个是 ex48_tests.py
[code brush:python;toolbar:false;]from nose.tools import *
import sys
sys.path.append("../ex48")
from ex48.ex48 import lexicon

def test_directions():
    assert_equal(lexicon.scan("north"), [('direction', 'north')])[/code]
回复

使用道具 举报

2

主题

7

帖子

7

积分

贫民

积分
7
Riario14 发表于 2016-10-27 20:06:19 | 显示全部楼层
我也在用这本书入门
贴一下我的代码
这是扫描器
  1. lexicon = {
  2.         'north': ('direction', 'north'),
  3.         'south': ('direction', 'south'),
  4.         'east': ('direction', 'east'),
  5.         'west': ('direction', 'west'),
  6.        
  7.         'go': ('verb', 'go'),
  8.         'kill': ('verb', 'kill'),
  9.         'eat': ('verb', 'eat'),
  10.        
  11.         'the': ('stop', 'the'),
  12.         'in': ('stop', 'in'),
  13.         'of': ('stop', 'of'),
  14.        
  15.         'bear': ('noun', 'bear'),
  16.         'princess': ('noun', 'princess'),
  17.         }
  18.        
  19. def isnum(Num):
  20.         try:
  21.                 return int(Num)
  22.         except:
  23.                 return None


  24. def scan(sentence):
  25.         words = sentence.split()
  26.         result = []
  27.        
  28.         for word in words:
  29.                 if isnum(word):
  30.                         result.append(('number', int(word)))
  31.                 elif word in lexicon.keys():
  32.                         result.append(lexicon[word])
  33.                 else:
  34.                         result.append(('error', word))
  35.         return result

复制代码

下面是测试代码
  1. from nose.tools import *
  2. from ex48 import lexicon


  3. def test_direction():
  4.         assert_equal(lexicon.scan("north"), [('direction', 'north')])
  5.         result = lexicon.scan("north south east")
  6.         assert_equal(result, [('direction', 'north'),
  7.                                                   ('direction', 'south'),
  8.                                                   ('direction', 'east')])
  9.                                                   
  10. def test_verbs():
  11.         assert_equal(lexicon.scan("go"), [('verb', 'go')])
  12.         result = lexicon.scan("go kill eat")
  13.         assert_equal(result, [('verb', 'go'),
  14.                               ('verb', 'kill'),
  15.                                                   ('verb', 'eat')])
  16.                                

  17. def test_stops():
  18.         assert_equal(lexicon.scan("the"), [('stop', 'the')])
  19.         result = lexicon.scan("the in of")
  20.         assert_equal(result, [('stop', 'the'),
  21.                                                   ('stop', 'in'),
  22.                                                   ('stop', 'of')])
  23.        
  24.        
  25. def test_noun():
  26.         assert_equal(lexicon.scan("bear"), [('noun', 'bear')])
  27.         result = lexicon.scan("bear princess")
  28.         assert_equal(result, [('noun', 'bear'),
  29.                               ('noun', 'princess')])

  30. def test_numbers():
  31.         assert_equal(lexicon.scan("1234"), [('number', 1234)])
  32.         result = lexicon.scan("3 91234")
  33.         assert_equal(result, [('number', 3),
  34.                               ('number', 91234)])

  35. def test_errors():
  36.         assert_equal(lexicon.scan("ASDFAFASDF"),[('error', 'ASDFAFASDF')])
  37.         result = lexicon.scan("bear IAS princess")
  38.         assert_equal(result, [('noun', 'bear'),
  39.                               ('error', 'IAS'),
  40.                                                   ('noun', 'princess')])
复制代码
回复 支持 1 反对 0

使用道具 举报

50

主题

1057

帖子

1108

积分

管理员

Rank: 9Rank: 9Rank: 9

积分
1108

活跃会员热心会员最佳导师默默耕耘突出贡献优秀版主荣誉管理论坛元老

blueelwang 发表于 2016-10-27 22:23:48 | 显示全部楼层
Riario14 发表于 2016-10-27 20:06
我也在用这本书入门
贴一下我的代码
这是扫描器

{:8_204:}
回复 支持 反对

使用道具 举报

您需要登录后才可以回帖 登录 | 立即注册

本版积分规则

快速回复 返回顶部 返回列表