尽管输出正确,但 Doctest 失败
我的功能是
def validate_latitude(lat):
"""Enforce latitude is in range
>>> validate_latitude(65)
65
>>> validate_latitude(91)
90
>>> validate_latitude(-91)
-90
"""
lat = min(lat, 90)
lat = max(lat, -90)
return lat
测试失败并显示此输出
**********************************************************************
File "packages/utils.py", line 64, in __main__.validate_latitude
Failed example:
validate_latitude(-91)
Expected:
-90
Got:
-90
**********************************************************************
尽管具有所需的输出,但仍无法看出为什么它会失败
My function is
def validate_latitude(lat):
"""Enforce latitude is in range
>>> validate_latitude(65)
65
>>> validate_latitude(91)
90
>>> validate_latitude(-91)
-90
"""
lat = min(lat, 90)
lat = max(lat, -90)
return lat
And the test fails with this output
**********************************************************************
File "packages/utils.py", line 64, in __main__.validate_latitude
Failed example:
validate_latitude(-91)
Expected:
-90
Got:
-90
**********************************************************************
Cant see why it fails in spite of having the deisred output
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
空白?
如果我突出显示您的输出,我可以看到“预期”值后面有额外的空格。不确定这是否相关。
Whitespace?
If I highlight your output, I can see additional whitespace following the "Expected" value. Not sure whether this is relevant or not.
在这两行中:
-90
中的-
之前有一个制表符,0
之后有四个空格字符。当 doctests 运行此代码时,当然不会产生额外的空格,因此相等比较失败。好的编辑器,例如 vim,有办法突出显示尾随空格和杂散制表符,这样您就不会遇到此类意外。不确定您正在使用什么编辑器或如何设置它,因此很难提供更具体的建议(除了确保您使用具有此类功能的编辑器并启用相关功能的明显建议之外;-)。
In these two lines:
You have a Tab character before the
-
in-90
, and four space characters after the0
. When doctests runs this code the extra whitespace is of course not produced, so the equality comparison fails.Good editors, e.g. vim, have ways to highlight trailing spaces, and stray tabs, so that you don't fall afould of such accidents. Not sure what editor you're using or how you have set it up, so it's hard to give more specific advice (besides the obvious one of ensuring you use an editor WITH such capabilities, and enable the capabilities in question;-).