zoukankan      html  css  js  c++  java
  • python流程控制:while循环

    python编程中whihe语句用于循环执行程序,即在某条件下,循环执行某段程序,以处理需要重复处理的相同任务。

    while循环语句格式:

    while <判断条件>:
        执行语句
    count = 0
    while (count <9):
        print("The count is:",count)
        count = count + 1
    else:
        print("Good bye!")

    无限循环:

    如果条件判断语句永远为Ture,循环将会无限的执行下去:

    m = 1
    while m == 1:
        num = int(input("Enter a number:"))
        print("you enterd: ",num)
    print("Good bey!")

    循环使用else语句:

      在python中,for else,else中的语句会在循环正常执行完的情况下执行,while...else也是这样的。

    count = 0
    while count < 5:
        print(count,"is less than 5")
        count = count +1
    else:
        print(count,"is not less than 5")

    以上输出结果为:

    0 is less than 5
    1 is less than 5
    2 is less than 5
    3 is less than 5
    4 is less than 5
    5 is not less than 5

    简语句组:

    类似if语句,如果你的while循环体只有一条语句,那么可以将该语句与while写在同一行:

    m = 1
    while (m):print("Give flag is really true")
    print("Good bey")
  • 相关阅读:
    Bootstrap-select 动态绑定值
    Centos 下载
    设置图片大小和旋转
    aolication全局传递参数
    Activity静态变量传递参数
    Activity传递参数
    Activity横屏后声明周期的变化
    Intent隐式意图
    Intent显示意图的四种跳转
    SharedPreferences简单的应用
  • 原文地址:https://www.cnblogs.com/liuyam/p/6290587.html
Copyright © 2011-2022 走看看