编写一个Python求1+2+3+…+100
时间: 2023-09-27 19:08:19 浏览: 91
方法一:使用for循环
```python
sum = 0
for i in range(1, 101):
sum += i
print(sum)
```
方法二:使用while循环
```python
sum = 0
i = 1
while i <= 100:
sum += i
i += 1
print(sum)
```
方法三:使用递归
```python
def sum(n):
if n == 1:
return 1
return n + sum(n-1)
print(sum(100))
```
阅读全文