這篇文章將為大家詳細講解有關python循環結構中如何使用else,文章內容質量較高,因此小編分享給大家做個參考,希望大家閱讀完這篇文章后對相關知識有一定的了解。

專注于為中小企業提供成都做網站、網站設計服務,電腦端+手機端+微信端的三站合一,更高效的管理,為中小企業元寶免費做網站提供優質的服務。我們立足成都,凝聚了一批互聯網行業人才,有力地推動了數千家企業的穩健成長,幫助中小企業通過網站建設實現規模擴充和轉變。
循環組合中的else執行的情況下是循環正常結束(即不是使用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')執行結果如下:
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
關于python循環結構中如何使用else就分享到這里了,希望以上內容可以對大家有一定的幫助,可以學到更多知識。如果覺得文章不錯,可以把它分享出去讓更多的人看到。
網站標題:python循環結構中如何使用else
新聞來源:http://www.yijiale78.com/article24/ihojje.html
成都網站建設公司_創新互聯,為您提供、靜態網站、App設計、軟件開發、微信公眾號、動態網站
聲明:本網站發布的內容(圖片、視頻和文字)以用戶投稿、用戶轉載內容為主,如果涉及侵權請盡快告知,我們將會在第一時間刪除。文章觀點不代表本網站立場,如需處理請聯系客服。電話:028-86922220;郵箱:631063699@qq.com。內容未經允許不得轉載,或轉載時需注明來源: 創新互聯