企业🤖AI Agent构建引擎,智能编排和调试,一键部署,支持私有化部署方案 广告
# break语句 `break`语句是用来 终止 循环语句的,即哪怕循环条件没有称为`False`或序列还没有被完全递归,也停止执行循环语句。 一个重要的注释是,如果你从`for`或`while`循环中 终止 ,任何对应的循环`else`块将**不**执行。 ``` #!/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](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'`。通过 终止 循环到达程序结尾来停止程序。 输入字符串的长度通过内建的`len`函数取得。 记住,`break`语句也可以在`for`循环中使用。 ## G2的Python诗 我在这里输入的是我所写的一段小诗,称为**G2的Python诗**: ``` Programming is fun When the work is done if you wanna make your work also fun:       use Python! ```