18.使用for循环计算+1-3+5-7+9-11+13...99的结果

j = 1 # -1
num1 = 0 #1-3
for i in range(1,100,2):
num1 += j * i # -3
j = j * -1 # 1
print(num1)

sum1 = 0
count = 1
for el in range(1,100,2):
if el % 4 == 1:
sum1 += el
else:
sum1 -= el
print(sum1)

print(el,end=" ")
if count % 2 == 1:
sum1 += el
else:
sum1 -= el
count += 1
print(sum1)

posted @ 2019-06-18 20:57  God_with_us  阅读(1207)  评论(0编辑  收藏  举报