Python入门:求1-2+3-4+5...99的所有数的和

1 num =1
2 sum =0
3 while num <=99:
4      if num % 2 ==1:
5          sum = sum + num
6      num =num +1
7 print(sum)

2.求1-2+3-4+5...99的所有数的和

1 num =1
2 sum =0
3 while num <=99:
4      if num % 2 ==1:
5          sum = sum + num
6      else:
7          sum =sum -num
8      num =num +1
9 print(sum)

 

posted @ 2018-05-02 22:27  zhang.ning  阅读(5189)  评论(0编辑  收藏  举报