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

如题所述

用Python的for循环实现等差序列相加,例如(1+2+3+4+5+...+20)这样的算法,代码如下:

import math
temp = 0
for x in range(1,20):
    temp += x
        break
     else:
         print temp
 #输入结果:210
温馨提示:答案为网友推荐,仅供参考
第1个回答  2011-08-22
是实现等差序列吗
如1+2+3+4+5.。。。这样的
>>> temp = 0
>>> for x in range(1,11):
temp += x
这样就实现了,很简短。呵呵
http://proupy.com本回答被提问者采纳
相似回答