Discuss / Python / 哇

文档测试

def fact(n): ''' Calculate 12...*n

>>> fact(1)
1
>>> fact(10)
3628800
>>> fact(-1)
Traceback (most recent call last):
...
ValueError
'''
if n < 1:
    raise ValueError()
if n == 1:
    return 1
return n * fact(n - 1)

if name == 'main': import doctest doctest.testmod()


  • 1

Reply