-3

I'm trying to get this doctest to pass by adding a function body but I don't know how to do that can anyone help me

def reverse(s):

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

if __name__ == "__main__":
    import doctest
    doctest.testmod(verbose=True)
joneshf
  • 2,296
  • 1
  • 20
  • 27
  • 1
    Are you asking how would you implement a function that reverses a string? Wow that is a clever way around the longstanding expectation in StackOverflow that you can't ask people to do work for you you have to try and ask for help when you are stuck. All right I'll bite, what have you tried? – Jason Sperske May 16 '13 at 05:02
  • Strings in python can be reversed like this "happy"[::-1] – Jason Sperske May 16 '13 at 05:03
  • So what I'm trying to do is get this to pass through a doctest but I can't find the right way to write a function body to reverse to make the doctest pass. – user2388433 May 16 '13 at 05:08

1 Answers1

0

With the above edit the function should work as expected. The output for the same when executed would look like this

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.
rgk
  • 858
  • 16
  • 28
  • Don't edit the original question like that, it makes the question confusing for other readers. Just include your answer in ... uh... your answer – wim May 16 '13 at 05:33
  • sure wim, will do it the next time – rgk May 16 '13 at 05:40
  • sorry first time on here and first time using python, I did manage to get the answer! Thank You – user2388433 May 16 '13 at 05:46