Python/gloss python for break

来自菜鸟教程
跳转至:导航、​搜索

<languages />

打破Python

中断声明

随着 打破 语句,我们可以在循环遍历所有项目之前停止循环:

退出循环时 x 是“香蕉”:

  fruits = ["apple", "banana", "cherry"]
for x in fruits:
  print(x)
  
  if x == 
  "banana":
    break

退出循环时 x 是“香蕉”,但是这次是在打印前休息了:

  fruits = ["apple", "banana", "cherry"]
for x in fruits:
  if x == 
  "banana":
    break
  print(x)