Discuss / Python / 打卡

打卡

Topic source

°Destiny

#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