【问题标题】:Python doctest says failed but returns correct valuePython doctest 说失败但返回正确的值
【发布时间】:2020-12-10 07:47:18
【问题描述】:

我在 PyCharm 中执行了一些文档测试,并在 python 控制台中运行了测试。它通过一个而另一个失败,但失败表明返回的值是正确的值。有谁知道我可能遗漏了什么,或者这是否是 PyCharm 中的一个已知错误等?

这里是代码

def friend_date(a, b):
    """
    Returns True if they have any hobbies in common, False is not.

    >>> elmo = ('Elmo', 5, ['hugging', 'being nice'])
    >>> sauron = ('Sauron', 5000, ['killing hobbits', 'chess'])
    >>> gandalf = ('Gandalf', 10000, ['waving wands', 'chess'])

    >>> friend_date(elmo, sauron)
    False

    >>> friend_date(sauron, gandalf)
    True 
    """
    return bool(set(a[2]) & set(b[2]))

【问题讨论】:

  • 您的True 行有一个尾随空格。这很重要。
  • 没有意识到空格在 python 中很重要,只是认为正确缩进很重要。非常感谢@user2357112supportsMonica

标签: python doctest


【解决方案1】:

确实正如@user2357112 指出的那样,您在第二个测试用例的预期输出中有空间。我用这段代码测试过,它工作正常:

def friend_date(a, b):
    """
    Returns True if they have any hobbies in common, False is not.

    >>> elmo = ('Elmo', 5, ['hugging', 'being nice'])
    >>> sauron = ('Sauron', 5000, ['killing hobbits', 'chess'])
    >>> gandalf = ('Gandalf', 10000, ['waving wands', 'chess'])

    >>> friend_date(elmo, sauron)
    False

    >>> friend_date(sauron, gandalf)
    True
    """
    return bool(set(a[2]) & set(b[2]))

【讨论】:

  • @brent-franklin 如果它回答了您的问题,请将其标记为已解决。
猜你喜欢
  • 2018-07-29
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2021-12-19
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多