def fact(n):
'''
Simple factorial function with doctest.
>>> fact(5)
120
>>> fact(1)
1
>>> fact(0)
Traceback (most recent call last):
...
ValueError: 0 is not allowed
'''
if n < 1:
raise ValueError('0 is not allowed')
if n == 1:
return 1
return n * fact(n - 1)
if __name__ == '__main__':
import doctest
doctest.testmod()
米粽粽
BTW:为什么评论里的代码没有语法高亮呢?