溫馨提示×

溫馨提示×

您好,登錄后才能下訂單哦!

密碼登錄×
登錄注冊×
其他方式登錄
點擊 登錄注冊 即表示同意《億速云用戶服務條款》

Python學習筆記之Break和Continue用法分析

發布時間:2020-10-16 18:11:15 來源:腳本之家 閱讀:203 作者:Johnny丶me 欄目:開發技術

本文實例講述了Python學習筆記之Break和Continue用法。分享給大家供大家參考,具體如下:

Python 中的Break 和 Continue

  • break:控制何時循環應該結束
  • continue: 跳過循環的一次迭代

Break 和 Continue[示例練習]

用 break 語句寫一個循環,用于創建剛好長 140 個字符的字符串 news_ticker。你應該通過添加 headlines 列表中的新聞標題創建新聞提醒,在每個新聞標題之間插入空格。如果有必要的話,從中間截斷最后一個新聞標題,使 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的區別:

1、break:終止,跳出,結束循環(可以作用在任何地方)。常與switch分支結構合用。

2、continue:結束本次的循環,進入下一次的循環(只能運用到循環結構中)。

關于Python相關內容感興趣的讀者可查看本站專題:《Python函數使用技巧總結》、《Python面向對象程序設計入門與進階教程》、《Python數據結構與算法教程》、《Python字符串操作技巧匯總》、《Python編碼操作技巧總結》及《Python入門與進階經典教程》

希望本文所述對大家Python程序設計有所幫助。

向AI問一下細節

免責聲明:本站發布的內容(圖片、視頻和文字)以原創、轉載和分享為主,文章觀點不代表本網站立場,如果涉及侵權請聯系站長郵箱:is@yisu.com進行舉報,并提供相關證據,一經查實,將立刻刪除涉嫌侵權內容。

AI

亚洲午夜精品一区二区_中文无码日韩欧免_久久香蕉精品视频_欧美主播一区二区三区美女