Python doctest错误

3

你好,我最近开始尝试使用Python,并正在阅读《像计算机科学家一样思考:学习Python第二版》。我在使用doctest时遇到了一些麻烦。我使用的是Windows 7操作系统和带有pydev插件的Eclipse IDE。

我的问题是,当我运行下面的脚本时,会出现以下错误。错误信息如下所示:

    Traceback (most recent call last):
  File "C:\Users\shaytac\PythonProjects\test.py", line 21, in <module>
    doctest.testmod()
  File "C:\Python26\lib\doctest.py", line 1829, in testmod
    for test in finder.find(m, name, globs=globs, extraglobs=extraglobs):
  File "C:\Python26\lib\doctest.py", line 852, in find
    self._find(tests, obj, name, module, source_lines, globs, {})
  File "C:\Python26\lib\doctest.py", line 906, in _find
    globs, seen)
  File "C:\Python26\lib\doctest.py", line 894, in _find
    test = self._get_test(obj, name, module, globs, source_lines)
  File "C:\Python26\lib\doctest.py", line 978, in _get_test
    filename, lineno)
  File "C:\Python26\lib\doctest.py", line 597, in get_doctest
    return DocTest(self.get_examples(string, name), globs,
  File "C:\Python26\lib\doctest.py", line 611, in get_examples
    return [x for x in self.parse(string, name)
  File "C:\Python26\lib\doctest.py", line 573, in parse
    self._parse_example(m, name, lineno)
  File "C:\Python26\lib\doctest.py", line 631, in _parse_example
    self._check_prompt_blank(source_lines, indent, name, lineno)
  File "C:\Python26\lib\doctest.py", line 718, in _check_prompt_blank
    line[indent:indent+3], line))
ValueError: line 2 of the docstring for __main__.compare lacks blank after >>>: '>>>compare(5, 4) '

def compare(a, b):
"""
  >>>compare(5, 4) 
  1
  >>>compare(7, 7)
  0
  >>>compare(2, 3)
  -1
  >>>compare(42, 1)
  1
"""
if a > b :
    return 1
if a == b :
    return 0
if a < b :
    return -1

if __name__ == '__main__':
    import doctest
    doctest.testmod()
1个回答

6

您应该编写:

>>> compare(5, 4)

正如错误提示所说:在compare之前缺少空格。

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接