我怎样才能通过这个doctest

时间:2013-05-16 04:59:27

标签: python doctest

我试图通过添加一个函数体来通过这个doctest,但我不知道该怎么做 任何人都可以帮助我

def reverse(s):

    """
    >>> reverse("happy")
    'yppah'
    >>> reverse("Python")
    'nohtyP'
    >>> reverse("")
    ''
    >>> reverse("P")
    'P'
    """

if __name__ == "__main__":
    import doctest
    doctest.testmod(verbose=True)

1 个答案:

答案 0 :(得分:0)

通过上面的编辑,该功能应该按预期工作。执行时相同的输出将如下所示

Trying:
    reverse("happy")
Expecting:
    'yppah'
ok
Trying:
    reverse("Python")
Expecting:
    'nohtyP'
ok
Trying:
    reverse("")
Expecting:
    ''
ok
Trying:
    reverse("P")
Expecting:
    'P'
ok
1 items had no tests:
__main__
1 items passed all tests:
4 tests in __main__.reverse
4 tests in 2 items.
4 passed and 0 failed.
Test passed.