小编Whe*_*-pi的帖子

ex48艰难地学习Python

direction = ['north', 'south', 'east', 'west', 'down', 'up', 'left', 'right', 'back']
verbs     = ['go', 'stop', 'kill', 'eat']
stop      = ['the', 'in', 'of', 'from', 'at', 'it']
nouns     = ['door', 'bear', 'princess', 'cabinet']
numbers   = [i for i in range(10)]


class lexicon(object):

    def scan(self, sentence):
        self.sentence = sentence
        self.words    = sentence.split()
        for word in self.words:
            if word is direction:
                word = ('direction','%s' % word)

            return word
Run Code Online (Sandbox Code Playgroud)

http://learnpythonthehardway.org/book/ex48.html是我正在研究的,我不知道为什么我的程序没有通过测试.当我运行nosetests时,我得到了这个错误.

ERROR: tests.ex48_tests.test_directions
----------------------------------------------------------------------
Traceback (most recent call last):
  File "/Users/tplaw/Public/projects/installs/venv/lib/python2.7/site-packages/nose/case.py", line 197, in runTest …
Run Code Online (Sandbox Code Playgroud)

python nosetests

3
推荐指数
1
解决办法
2080
查看次数

为什么我的代码没有正确创建元组列表?

direction = ['north', 'south', 'east', 'west', 'down', 'up', 'left', 'right', 'back']
class Lexicon(object):

    def scan(self, sentence):
        self.sentence = sentence
        self.words    = self.sentence.split()
        self.term = []

        for word in self.words:
            if word in direction:
                part = ('direction','%s' % word)
                self.term.append(word)
            return self.term


lexicon = Lexicon()
Run Code Online (Sandbox Code Playgroud)

当我进入时,lexicon.scan('north south east')我期待着回归给我[('direction','north'),('direction','south'),('direction','east')].相反,我得到了['north'].这是我希望程序整体上做的事情.

  1. 判刑.
  2. 对该句子使用扫描并将句子分成不同的单词.
  3. 扫描检查句子中的所有单词与几个列表(这只是单个列表上的第一个测试).
  4. 如果在列表中找到一个单词,那么我想创建一个元组,第一个术语是列表的名称,第二个术语是单词.
  5. 我想为不在列表中的单词创建一个元组,就像前一个但是使用"Error"而不是列表名称.
  6. 我想返回一个名为term的元组列表,其中包含所有不同的单词,其列表名称或错误位于元组的第一部分

python methods tuples

0
推荐指数
1
解决办法
83
查看次数

如果数字是'字符串',如何检查数字是否为数字?

这是一个更大的计划的一部分.这是我想要做的.

  1. 将句子传递给扫描方法.
  2. 请句子包含数字.
  3. 将句子分成不同的术语.
  4. 将一个元组附加到列表中,元组中的第一个表达式是单词或句子元素所适合的类型,第二个是单词或数字.

这是我正在尝试的:

def scan(self, sentence):
    self.term = []

    for word in sentence.split():
        if word in direction:
            self.term.append(('direction', word))
        elif word in verbs:
            self.term.append(('verb', word))
        elif word in stop:
            self.term.append(('stop', word))
        elif word in nouns:
            self.term.append(('noun', word))
        elif type(int(word)) == 'int':
            self.term.append(('number', int(word)))
        else:
            self.term.append(('error', word))

    return self.term



print lexicon.scan('12 1234')
Run Code Online (Sandbox Code Playgroud)

这是一个类中的方法,print语句在外面.我关心和遇到麻烦的部分是这样的:

elif type(int(word)) == int:
    self.term.append(('number', int(word)))
Run Code Online (Sandbox Code Playgroud)

它适用于任何自然数[1,无穷大]

编辑:当我尝试扫描时遇到问题('ASDFASDFASDF')

python types integer

0
推荐指数
1
解决办法
576
查看次数

标签 统计

python ×3

integer ×1

methods ×1

nosetests ×1

tuples ×1

types ×1