初识while循环

while 条件:

  代码块

print()

定时器:import time ,time.sleep(1),等待1秒

n=True

while n:

  print('1')

  time.sleep(1)

  n=False

print('end')

=============================================

import time 

kaishi = 1

flag=True

while flag:

  print(kaishi)

  if kaishi == 10

    flag=false

  kaishi=kaishi + 1

  time.sleep(1)

print('end')

============================================

break ---用于跳出循环

kaishi =1

while True:

  print(kaishi)

  if kaishi ==10

    break

  kaishi = kaishi +1

============================================

continue---用于跳出本次循环,继续下次循环

while True:

  print('123')

  continue

  print('456')

只能输入123,一直输出,而不能输出456

==========================================

输出1到10

start = 1

while True:

  print(start)

  if start == 10:

    break

  #start = start + 1

  start +=1

==========================================

输出1 2 3 4 5 6 8 9 10

start = 1

while True:

  if start ==7:

    start +=1

    continue

  print(start)

  if start == 10:

    break

  #start = start + 1

  start +=1

==========================================

1 到 100 所有数的和

i=1

sum=0

while true:

  print(i)

  sum = sum + i

  if i ==100:

    break

  i=i+1

print(sum)

==========================================

1 到 100 所有奇数的和

i=1

sum=0

while true:

  print(i)

  if i % 2 ==1:

    sum = sum + i

  if i ==100:

    break

  i=i+1

print(sum)

==========================================

1 到 100 所有偶数的和

i=1

sum=0

while true:

  print(i)

  if i % 2 ==0:

    sum = sum + i

  if i ==100:

    break

  i=i+1

print(sum)

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

  

 

posted @ 2018-06-22 16:47  黄瓜不是好瓜  阅读(141)  评论(0编辑  收藏  举报