zhu894532094 发表于 2018-8-6 10:26:26

python中的while...else-pengwei59的博客

  和其他语言不一样,除了和if搭配,else还可以和while搭配,不过意思不是条件判断了。
  比如:(在没有break情况下):
  a=0
  while a<=5:
  print(&quot;loop&quot;,a)
  a+=1
  else:
  print(&quot;yes&quot;)
  print(&quot;done&quot;)
  输出效果为:

  (在有break情况下):
  a=0
  while a<=5:
  print(&quot;loop&quot;,a)
  a+=1
  break
  else:
  print(&quot;yes&quot;)
  print(&quot;done&quot;)
  输出效果为:

  总结:
  while:
  else:
  语句可以检测代码又没有被中断。如果没有被中断,则会输出else后的语句,如有中断,则else后的语句不会被输出。
页: [1]
查看完整版本: python中的while...else-pengwei59的博客