python.输出1! +2! +3! +......n!
时间: 2023-12-02 10:03:21 浏览: 176
python 求1+2!+3!+...+20!的和 (示例)
5星 · 资源好评率100%
以下是Python代码,用于计算1! + 2! + 3! + …… + n!:
```python
def factorial(n):
if n == 0:
return 1
else:
return n * factorial(n-1)
n = int(input("请输入一个正整数:"))
sum = 0
for i in range(1, n+1):
sum += factorial(i)
print("1! + 2! + 3! + ... + n! = ", sum)
```
阅读全文