Skip to content

Instantly share code, notes, and snippets.

@nrchandan
Created October 8, 2014 09:03
Show Gist options
  • Select an option

  • Save nrchandan/55862b3bc1e5c1be2517 to your computer and use it in GitHub Desktop.

Select an option

Save nrchandan/55862b3bc1e5c1be2517 to your computer and use it in GitHub Desktop.
Doctest for factorial function.
def fact(n):
"""
Computes factorial.
>>> fact(1)
1
>>> fact(5)
120
>>> fact(-1) # doctest: +IGNORE_EXCEPTION_DETAIL
Traceback (most recent call last):
...
ArithmeticError: ...
"""
if n > 1:
return n * fact(n - 1)
elif n == 1 or n == 0:
return 1
else:
raise(ArithmeticError("Undefined for negative numbers"))
if __name__ == '__main__':
import doctest
doctest.testmod()
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment