Discuss / Python / 练习

练习

Topic source

米粽粽

#1 Created at ... [Delete] [Delete and Lock User]
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:为什么评论里的代码没有语法高亮呢?


  • 1

Reply