Python递归函数实现任意正整数阶乘的结果

代码示例

def re_func(n):
    '''
    :arg n: 需要计算阶乘的数字
    '''
    if n == 1:
        return 1
    else:
        return n * re_func(n - 1)


if __name__ == '__main__':
    num = 5
    result = re_func(num)
    print('{}的阶乘 = {} = {}'.format(num, '*'.join([str(i) for i in range(1, num + 1)]), result))

运行结果

5的阶乘 = 1*2*3*4*5 = 120