簡體   English   中英

Python doctest錯誤

[英]Python doctest error

嗨,我最近開始嘗試使用python,目前正在閱讀“像計算機科學家一樣思考:學習python v2nd版本”,我在doctest方面遇到了一些麻煩。 我將Windows 7計算機和Eclipse IDE與pydev一起使用。

我的問題是,當我運行以下腳本時,出現以下錯誤。 該腳本位於錯誤消息下方

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

你應該寫:

>>> compare(5, 4)

如錯誤所述: compare前缺少空白

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM