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")

 

转载于:https://www.cnblogs.com/liuyam/p/6290587.html

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值