【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

 

 

 

转载于:https://www.cnblogs.com/xiedoudou/p/5242104.html

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值