python利用函数的嵌套调用计算:1!+2!+3!+4!+5!
时间: 2023-12-29 10:05:35 浏览: 94
python 求1+2!+3!+...+20!的和 (示例)
5星 · 资源好评率100%
以下是利用函数的嵌套调用计算1!+2!+3!+4!+5!的Python代码:
```python
def factorial(n):
if n == 1:
return 1
else:
return n * factorial(n-1)
def sum_factorial(n):
sum = 0
for i in range(1, n+1):
sum += factorial(i)
return sum
print(sum_factorial(5)) # 输出:153
```
阅读全文