艰苦学习python ex48[内存错误]

艰苦学习python ex48[内存错误],python,Python,我已经完成了下面的代码(还没有完成数字和错误扫描部分),但是测试结果告诉我有一个内存错误。我不知道怎么了 这是我的代码: class lexicon(object): def __init__(self): pass def scan(self, sentence): direction = ["north", "south", "east", "west", "dwon", "up", "left", "right", "back"] verb = ["go",

我已经完成了下面的代码(还没有完成数字和错误扫描部分),但是测试结果告诉我有一个内存错误。我不知道怎么了

这是我的代码:

class lexicon(object):

def __init__(self):
    pass

def scan(self, sentence):

    direction = ["north", "south", "east", "west", "dwon", "up", "left", "right", "back"]
    verb = ["go", "stop", "kill", "eat"]
    stop = ["the", "in", "of", "from", "at", "it"]
    noun = ["door", "bear", "princess", "cabinet"]
    word_type = [direction, verb, stop, noun]

    wordlist = sentence.split()
    result = []

    for a in wordlist:
        x = 0
        c = 0
        while x < len(word_type) and c == 0:
            b = word_type[x]

            if a not in b:
                x += 1
            else:
                result.append((b,a))
                c == 1

        if x == len(word_type):
            result.append(("error",a))

    return result
这就是结果:

ERROR: tests.lexicon_tests.test_directions
----------------------------------------------------------------------
Traceback (most recent call last):
  File "c:\users\sine\appdata\local\programs\python\python35-32\lib\site-packages\nose\case.py", line 198, in runTest
    self.test(*self.arg)
  File "E:\Python\exercises of learn python the hard way\ex48\tests\lexicon_tests.py", line 6, in test_directions
    assert_equal(lexicon1.scan("north"),[("direction","north")])
  File "E:\Python\exercises of learn python the hard way\ex48\game48.py", line 26, in scan
    result.append((b,a))
MemoryError

======================================================================
ERROR: tests.lexicon_tests.test_verbs
在这种情况下,
c
x
都不会改变,因此循环
当x
不断运行时,
result
会无限增长并消耗所有内存


我不确定你想要的是
c=1
还是
c+=1
,但不太可能是
c==1
,因为这是不可行的。你没有利用等式比较的结果。

你可能想要的是c=1,而不是c==1。在这种情况下,什么都不会改变,您将拥有无限循环

除了循环问题之外,还有另一个问题,
lexicon1.scan(“北”)
最终将返回:

[(["north", "south", "east", "west", "dwon", "up", "left", "right", "back"], "north")]
而不是所需的:
[('direction','north')]

我们可以解决这个问题,并简化代码,使用字典保存单词:

class lexicon(object):

    def __init__(self):
        self.word_types = {
            "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 scan(self, sentence):
        wordlist = sentence.split()
        result = []

        for word in wordlist:
            for word_type in self.word_types:
                if word in self.word_types[word_type]:
                    result.append((word_type, word))
                    break
            else:
                result.append(("error", word))

        return result
请注意,
for
循环上的
else
if
语句上的
else
不同,它可以被认为是“无中断”的意思,即当从未到达
break
语句且循环已结束时要执行的代码

[(["north", "south", "east", "west", "dwon", "up", "left", "right", "back"], "north")]
class lexicon(object):

    def __init__(self):
        self.word_types = {
            "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 scan(self, sentence):
        wordlist = sentence.split()
        result = []

        for word in wordlist:
            for word_type in self.word_types:
                if word in self.word_types[word_type]:
                    result.append((word_type, word))
                    break
            else:
                result.append(("error", word))

        return result