数组作为doctest中的预期结果。文本错误';不需要任何东西;。Python 3.3.2
我已尝试对以下来源使用doctest:数组作为doctest中的预期结果。文本错误';不需要任何东西;。Python 3.3.2,python,python-3.x,doctest,Python,Python 3.x,Doctest,我已尝试对以下来源使用doctest: def add_问候语(L=[]): “”“(列表)->NoneType 将“hello”附加到L并打印L。 >>>问候语\u list=['hi','bonjour'] >>>添加问候语(问候语列表) >>>问候你的名单 [“你好”,“你好”,“你好”] """ L.append('hello') 印刷品(L) 如果uuuu name uuuuuu='\uuuuuuu main\uuuuuuu': 进口医生测试 打印(doctest.testmod())
def add_问候语(L=[]):
“”“(列表)->NoneType
将“hello”附加到L并打印L。
>>>问候语\u list=['hi','bonjour']
>>>添加问候语(问候语列表)
>>>问候你的名单
[“你好”,“你好”,“你好”]
"""
L.append('hello')
印刷品(L)
如果uuuu name uuuuuu='\uuuuuuu main\uuuuuuu':
进口医生测试
打印(doctest.testmod())
当我像“python my_file.py”一样启动这个文件时,我得到了以下信息:
python my_file.py
**********************************************************************
File "my_file.py", line 9, in __main__.add_greeting
Failed example:
add_greeting(greetings_list)
Expected nothing
Got:
['hi', 'bonjour', 'hello']
**********************************************************************
1 items had failures:
1 of 3 in __main__.add_greeting
***Test Failed*** 1 failures.
TestResults(failed=1, attempted=3)
有人能帮我解决这个错误吗?
为什么我在这种情况下“什么都不期望”?
如何修复它?试试看
def add_greeting(L=[]):
""" (list) -> NoneType
Append 'hello' to L and print L.
>>> greetings_list = ['hi', 'bonjour']
>>> add_greeting(greetings_list)
['hi', 'bonjour', 'hello']
>>> greetings_list
['hi', 'bonjour', 'hello']
"""
L.append('hello')
print(L)
if __name__ == '__main__':
import doctest
print(doctest.testmod())
之所以会出现错误,是因为在
add\u greeting
的末尾有print(L)
,但是docstring中的示例控制台输出不希望通过add\u greeting
打印或返回任何内容,这很好!现在我明白我为什么会犯这样的错误了!谢谢!现在一切都好了!我应该把更多的注意力放在“失败的例子”部分的表达上。。。