while后边的条件永远为真(True),在python看来只有以下内容会被看作假:False、None、0、“”、‘’、()、[]、{}
1 while ‘‘: 2 print(‘进入循环‘) 3 print(‘退出循环‘) 4 ======5 退出循环
1 while False: 2 print(‘进入循环‘) 3 print(‘退出循环‘) 4 ======5 退出循环
当程序陷入死循环之后,按下CTRL+C可以终止程序
1 while ‘C‘: 2 print(‘hello‘) #程序会一直输出hello 3 ====== 4 hello 5 hello 6 hello 7 hello 8 hello 9 hello 10 hello 11 hello 12 hello 13 hello 14 hello 15 hello #按下CTRL+C之后结束程序 16 Traceback (most recent call last): 17 File "C:\Users\xcd\Desktop\1.py", line 2, in <module> 18 print(‘hello‘) 19 File "E:\program\python\lib\idlelib\PyShell.py", line 1344, in write 20 return self.shell.write(s, self.tags) 21 KeyboardInterrupt
通过调用random模块,利用里边的randint产生某个范围内的随机整数
1 import random 2 time =5 3 while time>0: 4 number = random.randint(1,100) 5 print(number) 6 time = time-1 7 ====== 8 96 9 54 10 49 11 11 12 14
原文:http://www.cnblogs.com/xiedoudou/p/5242104.html