Python 文档测试错误

发布于 2024-09-02 22:34:41 字数 1885 浏览 7 评论 0原文

您好,我最近开始尝试使用 python,目前正在阅读“像计算机科学家一样思考:学习 python v2nd 版”,我在 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()

Hi I recently started experimenting with python currently reading "Think like a computer scientist: Learning python v2nd edition" I have been having some trouble with doctest. I use a windows 7 machine and Eclipse IDE with pydev.

My question is when i run the script below i get the error below. Said script is below the the error message

    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()

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

晨光如昨 2024-09-09 22:34:41

你应该写:

>>> compare(5, 4)

正如错误所说: compare 之前缺少空白

you shoud write:

>>> compare(5, 4)

as the error said: missing blank before compare

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文