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