1 '''1、求1-100的所有数的和'''
 2 def add():
 3     i = sum = 0
 4     while i < 100:
 5         i += 1
 6         sum += i
 7     return sum
 8 print(add())
 9 
10 '''2、求1-2+3-4+5 ... 99的所有数的和'''
11 def addsub():
12     sum = i = 0
13     while i < 99:
14         i += 1
15         if i%2 == 1:
16             sum = sum + i
17         else:
18             sum = sum - i
19     print(sum,i)
20 addsub()

 

posted on 2018-12-26 11:19  无伞  阅读(799)  评论(0编辑  收藏  举报