#1、使用while循环输入 1 2 3 4 5 6 8 9 10 #方法一: # count = 1 # flag = True # while flag: # if count == 7: # print('') # count += 1 # print(count) # count = count + 1 # count += 1 # if count == 11: # flag = False #方法二: # count = 1 # flag = True # while flag: # # print(count) # if count == 7: # print('') # else:print(count) # count = count + 1 # count += 1 # if count == 11: # flag = False #方法三: # count = 1 # while count < 11: # if count == 7: # print('') # else:print(count) # count = count + 1 # count += 1
count = 1while count < 10: count += 1 if count == 7 continue else: print(count)
#2、求1-100的所有数的和 # count = 1 # sum = 0 # while count < 101: # # sum2 = count # sum = sum + count # sum += count # count = count + 1 # print(sum) #3、输出 1-100 内的所有奇数 #1,3,5,7,9.。。。。。 #方法一: # count = 1 # while count < 101: # print(count) # count += 2 #方法二: # count = 1 # while count < 101: # if count % 2 == 1: # print(count) # count += 1 # 5、求1-2+3-4+5 ... 99的所有数的和 n = 1 sum = 0 while n < 100: if n % 2 == 0: n = -n sum = sum + n if n % 2 == 0: n = -n n += 1 print('1-2+3-4+5 ... 99的所有数的和为%d'%sum)#相当于print(sum) # sum = 1 # add = 0 # while sum < 100: # if sum % 2 == 1: # add = add + sum # else: # add = add - sum # sum += 1 # print(add)