• 初识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)

      

  • 相关阅读:
    Codeforces Round #486 (Div. 3) F. Rain and Umbrellas
    Codeforces Round #486 (Div. 3) E. Divisibility by 25
    Codeforces Round #486 (Div. 3) D. Points and Powers of Two
    Codeforces Round #486 (Div. 3) C. Equal Sums
    Codeforces Round #486 (Div. 3) B. Substrings Sort
    Codeforces Round #486 (Div. 3) A. Diverse Team
    2018-06-08
    CCPC-2017-秦皇岛站
    洛谷 P4819 [中山市选]杀人游戏
    洛谷 P2721 小Q的赚钱计划
  • 原文地址:https://www.cnblogs.com/huangguabushihaogua/p/9214263.html
Copyright © 2020-2023  润新知