python中continue用法_Python学习笔记之Break和Continue用法分析
本文實例講述了Python學習筆記之Break和Continue用法。分享給大家供大家參考,具體如下:
Python 中的Break 和 Continue
break:控制何時循環(huán)應該結束
continue: 跳過循環(huán)的一次迭代
Break 和 Continue[示例練習]
用 break 語句寫一個循環(huán),用于創(chuàng)建剛好長 140 個字符的字符串 news_ticker。你應該通過添加 headlines 列表中的新聞標題創(chuàng)建新聞提醒,在每個新聞標題之間插入空格。如果有必要的話,從中間截斷最后一個新聞標題,使 news_ticker 剛好長 140 個字符
headlines = ["Local Bear Eaten by Man",
"Legislature Announces New Laws",
"Peasant Discovers Violence Inherent in System",
"Cat Rescues Fireman Stuck in Tree",
"Brave Knight Runs Away",
"Papperbok Review: Totally Triffic"]
news_ticker = ""
for item in headlines:
news_ticker += item + " "
if len(news_ticker) >= 140:
news_ticker = news_ticker[:140]
break
print(news_ticker) # Local Bear Eaten by Man Legislature Announces New Laws Peasant Discovers Violence Inherent in System Cat Rescues Fireman Stuck in Tree Brave
print(len(news_ticker)) # 140
運行結果:
Local Bear Eaten by Man Legislature Announces New Laws Peasant Discovers Violence Inherent in System Cat Rescues Fireman Stuck in Tree Brave
140
break與continue的區(qū)別:
1、break:終止,跳出,結束循環(huán)(可以作用在任何地方)。常與switch分支結構合用。
2、continue:結束本次的循環(huán),進入下一次的循環(huán)(只能運用到循環(huán)結構中)。
希望本文所述對大家Python程序設計有所幫助。
總結
以上是生活随笔為你收集整理的python中continue用法_Python学习笔记之Break和Continue用法分析的全部內容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: win7下开启梦幻桌面
- 下一篇: 起点中文网越来越火爆了,我利用Pytho