while循环应用
- 计算1~100的累积和(包含1和100)
参考代码如下:
#encoding=utf-8
i = 1
sum = 0
while i <= 100:
sum = sum + i
i += 1
print("1~100的累积和为:%d" % sum)
- 计算1~100之间偶数的累积和(包含1和100)
参考代码如下:
#encoding=utf-8
i = 1
sum = 0
while i <= 100:
if i % 2 == 0:
sum = sum + i
i+=1
print("1~100的累积和为:%d" % sum)
转载于:https://blog.51cto.com/13857166/2140628