鍍金池/ 教程/ Python/ 使用 break 語句
備份腳本——版本四
使用 <strong>init</strong> 方法
控制流
異常
表 15.1 一些特殊的方法
如何創(chuàng)建你自己的模塊
使用字典
前言
使用默認參數(shù)值
表 5.1 運算符與它們的用法
解決問題——編寫一個 Python 腳本
使用 for 語句
使用 continue 語句
使用元組輸出
對象與參考
使用函數(shù)形參
使用默認參數(shù)值
使用 if 語句
如何引發(fā)異常
使用源文件
使用對象的方法
使用表達式
定義函數(shù)
使用局部變量
使用列表綜合
使用 sys.argv
使用 lambda 形式
使用 global 語句
備份腳本——版本二
使用列表
使用 while 語句
備份腳本——版本一
使用元組
輸入/輸出
使用類與對象的變量
使用 sys 模塊
表 5.2 運算符優(yōu)先級
處理異常
使用 break 語句
函數(shù)
基本概念
運算符與表達式
介紹
使用文件
使用序列
接下來學習什么?
使用帶提示符的 Python 解釋器
使用 DocStrings
使用字面意義上的語句
最初的步驟
數(shù)據(jù)結構
儲存與取儲存
使用 dir 函數(shù)
模塊
Python 標準庫
備份腳本——版本三(不工作?。?/span>
創(chuàng)建一個類
安裝 Python
面向對象的編程
使用模塊的<strong>name</strong>
使用變量和字面意義上的常量
使用繼承

使用 break 語句


    #!/usr/bin/python
    # Filename: break.py

    while True:
    s = raw_input('Enter something : ')
    if s == 'quit':
    break
    print 'Length of the string is', len(s)
    print 'Done'

(源文件:code/break.py

輸出


    $ python break.py
    Enter something : Programming is fun
    Length of the string is 18
    Enter something : When the work is done
    Length of the string is 21
    Enter something : if you wanna make your work also fun:
    Length of the string is 37
    Enter something :   use Python!
    Length of the string is 12
    Enter something : quit
    Done

它如何工作

在這個程序中,我們反復地取得用戶地輸入,然后打印每次輸入地長度。我們提供了一個特別的條件來停止程序,即檢驗用戶的輸入是否是'quit'。通過 終止 循環(huán)到達程序結尾來停止程序。

輸入字符串的長度通過內(nèi)建的 len 函數(shù)取得。

記住,break 語句也可以在 for 循環(huán)中使用。

G2 的 Python 詩

我在這里輸入的是我所寫的一段小詩,稱為 G2 的 Python 詩


    Programming is fun
    When the work is done
    if you wanna make your work also fun:
      use Python!
上一篇:使用元組下一篇:使用 sys 模塊