Discuss / Python / ...省略内容

...省略内容

Topic source

青铜神裔

#1 Created at ... [Delete] [Delete and Lock User]
def fact(n):
    '''
    Calculate 1*2*...*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)

  • 1

Reply