我刚刚开始学习python,我正在关注大学里的一些在线讲座。我刚被介绍用脚本写博士论文。下面是我的代码:
""" Our first python source file. """
from operator import floordiv, mod
def divide_exact(n,d):
""" Return the quotient and remainder.
>>> q, r = divide_exact(2013, 10)
>>> q
201
>>> r
3
"""
return floordiv(n, d), mod(n, d)
当我跑的时候
python3 -m doctest -v lec3video3
我通过了0个测试用例。我得到这个错误
File "lec3video3test", line 7, in lec3video3test
Failed example:
q, r = divide_exact(2013, 10)
Exception raised:
Traceback (most recent call last):
File "/Library/Frameworks/Python.framework/Versions/3.7/lib/python3.7/doctest.py", line 1329, in __run
compileflags, 1), test.globs)
File "<doctest lec3video3test[0]>", line 1, in <module>
q, r = divide_exact(2013, 10)
NameError: name 'divide_exact' is not defined
我不知道为什么,因为我知道我的代码工作。我做错什么了?我已经在交互模式下测试了我的代码,一切都是正确的。你知道吗
当前版本的代码中有一个
IndentationError
。不过,我想这只是一个错误,发生在复制和粘贴,因为你会得到一个完全不同的错误消息,然后。你知道吗关于你描述的
NameError
:如果我把你给定的代码在文件
example.py
中并使用以下命令调用doctest
,我得到:上面写着
因为模块docstring中没有测试。除此之外,
divide_exact
函数docstring中的所有三个测试都按预期通过。你知道吗因此,您只需确保文件位于正确的文件夹中,并且命令遵循
doctest
模块所需的语法即可。你知道吗相关问题 更多 >
编程相关推荐