【Py】while循环为假条件、强制结束、产生随机数

While循环为假条件:

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

 

 

 

posted @ 2016-03-04 14:55  喜闻乐见小逗逗  阅读(715)  评论(0编辑  收藏  举报