Python中def函数整数a1到a2所以整数之和
时间: 2024-05-08 13:18:56 浏览: 42
当然可以,可以通过循环实现:
```python
def sum_of_integers(a1, a2):
if a1 > a2:
a1, a2 = a2, a1
total = 0
for i in range(a1, a2+1):
total += i
return total
```
使用示例:
```python
a1 = 1
a2 = 100
sum = sum_of_integers(a1, a2)
print("从", a1, "到", a2, "所有整数之和为:", sum)
```
输出为:
```
从 1 到 100 所有整数之和为: 5050
```
阅读全文