如何用python 中的for 循环 实现加法

如题所述

用Python的for循环实现等差序列相加,例如(1+2+3+4+5+...+20)这样的算法,代码如下:
1
2
3
4
5
6
7
8
import math
temp = 0
for x in range(1,20):
temp += x
break
else:
print temp
#输入结果:210
温馨提示:答案为网友推荐,仅供参考
相似回答