一、计算1-2+3-4+5...99的和
1 count = 1 2 sums = 0 3 while count < 100: 4 if count % 2 == 0: 5 sums = sums - count 6 else: 7 sums = sums + count 8 count = count + 1 9 print(sums)
二、Python break/continue
break 跳出当前循环,如:有两层循环,跳出当前循环,不会跳出外层循环,continue 结束本次循环,继续下次循环。
while 可以与else 同等级组合,while 条件,条件不满足时,执行else:后面的语句,如何中间有break 则不会执行else
count = 0 while count < 5: print("Loop",count) count += 1 else: print('循环已经走完!')