新聞中心
眾多語言中都有if else這對條件選擇組合,但是在python中還有更多else使用的地方,比如說循環(huán)for,或者while都可以和else組合。

創(chuàng)新互聯(lián)是一家集網(wǎng)站建設(shè),仙居企業(yè)網(wǎng)站建設(shè),仙居品牌網(wǎng)站建設(shè),網(wǎng)站定制,仙居網(wǎng)站建設(shè)報(bào)價(jià),網(wǎng)絡(luò)營銷,網(wǎng)絡(luò)優(yōu)化,仙居網(wǎng)站推廣為一體的創(chuàng)新建站企業(yè),幫助傳統(tǒng)企業(yè)提升企業(yè)形象加強(qiáng)企業(yè)競爭力??沙浞譂M足這一群體相比中小企業(yè)更為豐富、高端、多元的互聯(lián)網(wǎng)需求。同時(shí)我們時(shí)刻保持專業(yè)、時(shí)尚、前沿,時(shí)刻以成就客戶成長自我,堅(jiān)持不斷學(xué)習(xí)、思考、沉淀、凈化自己,讓我們?yōu)楦嗟钠髽I(yè)打造出實(shí)用型網(wǎng)站。
下面簡單介紹一下for-else while-else組合
循環(huán)組合中的else執(zhí)行的情況下是循環(huán)正常結(jié)束(即不是使用break退出)。如下列代碼:
numbers= [1,2,3,4,5]
for nin numbers:
if (n >5):
print('the value is %d '%(n))
break
else:
print('the for loop does not end with break')
i= 0
while(numbers[i] <5):
print('the index %d value is %d'%(i, numbers[i]))
if (numbers[i] <0) :
break
i= i+ 1
else:
print('the loop does not end with break')
numbers= [1,2,3,4,5]
for nin numbers:
if (n >5):
print('the value is %d '%(n))
break
else:
print('the for loop does not end with break')
i= 0
while(numbers[i] <5):
print('the index %d value is %d'%(i, numbers[i]))
if (numbers[i] <0) :
break
i= i+ 1
else:
print('the loop does not end with break')執(zhí)行結(jié)果如下:
C:\Python27>python.exe for_else.py thefor loop doesnot end withbreak the index0 valueis 1 the index1 valueis 2 the index2 valueis 3 the index3 valueis 4 the loop doesnot end withbreak
文章標(biāo)題:創(chuàng)新互聯(lián)Python教程:你所不知道的python循環(huán)中的else
網(wǎng)頁網(wǎng)址:http://www.dlmjj.cn/article/dhchpig.html


咨詢
建站咨詢
