编写函数,输出1~100中所有偶数和
时间: 2023-06-04 11:04:51 浏览: 1399
c代码-请编写函数sum,其功能是:用for循环语句求1到n之间所有偶数之和(若n为偶数包括n), 并将结果返回给主函数。(n值由主函数传入)
Sure, the function to output the sum of all even numbers from 1 to 100 can be written as follows:
```python
def sum_of_evens():
sum = 0
for i in range(1, 101):
if i % 2 == 0:
sum += i
print(sum)
```
When you call this function, it will output the sum of all even numbers from 1 to 100, which is 2550.
阅读全文