Python基础——continute与break(循环控制)
生活随笔
收集整理的這篇文章主要介紹了
Python基础——continute与break(循环控制)
小編覺得挺不錯(cuò)的,現(xiàn)在分享給大家,幫大家做個(gè)參考.
文章目錄
- 1.非continue、break情形
- 2.break用法
- 3.continue用法
1.非continue、break情形
True and False ,當(dāng)輸入1時(shí),a=False時(shí),會(huì)執(zhí)行接下來的語(yǔ)句后再跳出這個(gè)循環(huán)。
a = True
while a:b = int(input('please input a number:'))if b == 1:a = Falseelse:pass #pass表示什么都不做
print('finished!') #分別輸入2,3,4,1 ,當(dāng)輸入為非1時(shí),什么都不做。當(dāng)輸入為1時(shí)a為False,跳出循環(huán),打印finished!#輸出
please input a number:2
please input a number:3
please input a number:4
please input a number:1
finished!
2.break用法
break用法,在循環(huán)語(yǔ)句中,使用 break, 當(dāng)符合跳出條件時(shí),會(huì)直接結(jié)束循環(huán),這是 break 和 True False 的區(qū)別。
while True:b = int(input('please input a number:'))if b == 1:break #終止循環(huán)else:passprint('still in while') #非1的時(shí)候會(huì)打印此句
print('finished!') #終止循環(huán)即輸入為1的情況
#分別輸入2,3,4,1#輸出
please input a number:2
still in while
please input a number:3
still in while
please input a number:4
still in while
please input a number:1
finished!
3.continue用法
在代碼中,滿足b=1的條件時(shí),因?yàn)槭褂昧?continue , python 不會(huì)執(zhí)行 else 后面的代碼,而會(huì)直接進(jìn)入下一次循環(huán)(不是終止循環(huán))。
while True:b = int(input('please input a number:'))if b == 1:continue #當(dāng)輸入1時(shí)將不會(huì)執(zhí)行print('still in while')語(yǔ)句,但會(huì)繼續(xù)進(jìn)入下一個(gè)循環(huán),這是與break語(yǔ)句的不同之處else:passprint('still in while')
print('finished!')
#分別輸入2,3,4,1,1#輸出
please input a number:2
still in while
please input a number:3
still in while
please input a number:4
still in while
please input a number:1
please input a number:1
總結(jié)
以上是生活随笔為你收集整理的Python基础——continute与break(循环控制)的全部?jī)?nèi)容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: gc 吞吐量和停顿时间_GC对吞吐量和延
- 下一篇: 均方根误差有没有单位_1千万年仅误差一秒