while循环习题

9.循环

  死循环

  while 1==1:

    print('ok')

程序1-------------

import time
count = 0
while count < 10:
  print('ok',time.time())
  count = count + 1
print('final')

程序2-------------

求1--100的和

a = 1
b = 0
while a < 101:
  b = b + a
  a = a + 1
print(b)

程序3---------

输出1--100内所有的奇数

a = 1
while a < 101:
  if a % 2 == 1:
    print(a)
  else:
    pass
  a = a + 1

程序4-------------------

输出 1 2 3 4 5 6     8 9 10

a = 1
  while a < 11:
  if a != 7:
    print(a)
  else:
    pass
  a = a + 1

程序5---------

输出 1---100之内所有的偶数

a = 1
  while a < 101:
  if a % 2 ==0:
    print(a)
  else:
    pass
  a = a + 1

程序6------------------

输出1 - 2 + 3 - 4 + 5 - 6 …………- 98 + 99的结果

a = 1
b = 0
while a < 100:
  if a % 2 == 1:
    b = b + a
  else:
    b = b - a
  a = a + 1
print(b)

原文地址:https://www.cnblogs.com/lhqlhq/p/8609058.html