设为首页 收藏本站
查看: 1016|回复: 0

[经验分享] Python中的循环退出举例及while循环举例

[复制链接]

尚未签到

发表于 2018-8-11 07:38:38 | 显示全部楼层 |阅读模式
  循环退出
  for循环:
  for
  else
  for 循环如果正常结束,都会执行else语句。
  脚本1:
  #!/usr/bin/env python
  for i in xrange(10):
  print i
  else:
  print "main end"
  结果:
  [root@localhost 20171227]# python exit.py
  0
  1
  2
  3
  4
  5
  6
  7
  8
  9
  main end
  [root@localhost 20171227]#
  脚本2:
  #!/usr/bin/env python
  import time
  for i in xrange(10):
  print i
  time.sleep(1)
  else:
  print "main end"
  结果:(中途按ctrl+c中断)
  [root@localhost 20171227]# python exit.py
  0
  1
  2
  3
  4
  ^CTraceback (most recent call last):
  File &quot;exit.py&quot;, line 6, in <module>
  time.sleep(1)
  KeyboardInterrupt
  [root@localhost 20171227]#
  脚本3:
  没正常结束:
  #!/usr/bin/env python
  import time
  for i in xrange(10):
  print i
  if i == 5:
  break
  else:
  print &quot;main end&quot;
  结果:
  [root@localhost 20171227]# python exit.py
  0
  1
  2
  3
  4
  5
  [root@localhost 20171227]#
  脚本4:(跳过本次循环continue)
  #!/usr/bin/env python
  import time
  for i in xrange(10):
  if i == 3 :
  continue
  if i == 5:
  break
  print i
  else:
  print &quot;main end&quot;
  结果:
  [root@localhost 20171227]# python exit.py
  0
  1
  2
  4
  [root@localhost 20171227]#
  
  脚本5:(pass 什么都不作)
  #!/usr/bin/env python
  import time
  for i in xrange(10):
  if i == 3 :
  continue
  elif i == 5:
  break
  elif i ==6:
  pass   #类似shell 中的:
  print i
  else:
  print &quot;main end&quot;
  脚本6:
  #!/usr/bin/env python
  import time
  import sys
  for i in xrange(10):
  if i == 3 :
  continue
  elif i == 5:
  continue
  elif i ==6:
  pass
  elif i ==7:
  sys.exit()
  print i
  else:
  print &quot;main end&quot;
  print &quot;hahaha&quot;
  结果:
  [root@localhost 20171227]# python exit.py
  0
  1
  2
  4
  6
  [root@localhost 20171227]#
  PIP显示第三方包
  [root@localhost 20171227]# pip list
  DEPRECATION: The default format will switch to columns in the future. You can use --format=(legacy|columns) (or define a format=(legacy|columns) in your pip.conf under the
    section) to disable this warning.
      backports.ssl-match-hostname (3.4.0.2)
      chardet (2.2.1)
      configobj (4.7.2)
      decorator (3.4.0)
      iniparse (0.4)
      IPy (0.75)
      ipython (1.2.1)
      jsonschema (2.5.1)
      kitchen (1.1.1)
      langtable (0.0.31)
      matplotlib (1.2.0)
      作业:猜数字游戏
      系统生成一个20以内的随机整数。
      玩家有6次机会进行猜猜看,每次猜测都有反馈(猜大了,猜小了,猜对了-结束)
      6次中,猜对了,玩家赢了。
      否则系统赢了
      In [1]: import random
      In [2]: random.ran
      random.randint    random.random     random.randrange
      In [2]: random.randint(1,20)
      Out[2]: 14
      In [3]: random.randint(1,20)
      Out[3]: 6
      流程控制-while举例
      while与for相对比:
      for循环用在有次数的循环上。
      while循环用在有条件的控制上。
      while循环:
      while循环,直到表达式变为假,才退出。while循环,表达式是一个逻辑表达式,必须返回一个True或False
      语法:
      while expression:
      statement(s)
      练习脚本如果下:
      脚本1:
      #!/usr/bin/python
      n = 0
      while 1:
      if n == 10:
      break
      print n, 'hellow'
      n +=1
      结果:
      [root@localhost 20171227]# python while.py
      0 hellow
      1 hellow
      2 hellow
      3 hellow
      4 hellow
      5 hellow
      6 hellow
      7 hellow
      8 hellow
      9 hellow
      [root@localhost 20171227]#
      脚本2:
      #!/usr/bin/python
      while 1:
      print 'hellow'
      input=raw_input(&quot;please input sth,q for exit.&quot;)
      if input == &quot;q&quot;:
      break
      结果:
      [root@localhost 20171227]# python while.py
      hellow
      please input sth,q for exit.s
      hellow
      please input sth,q for exit.3
      hellow
      please input sth,q for exit.q
      [root@localhost 20171227]#
      条件 为假时也会退出:
      脚本3:
      #!/usr/bin/python
      sth=''
      while sth != 'q':
      print 'hellow'
      sth=raw_input(&quot;please input sth,q for exit.&quot;)
      脚本4:
      回车后退出:
      #!/usr/bin/python
      sth=''
      while sth != 'q':
      print 'hellow'
      sth=raw_input(&quot;please input sth,q for exit.&quot;)
      if not sth:
      break
      脚本5:
      #!/usr/bin/python
      sth=''
      while sth != 'q':
      print 'hellow'
      sth=raw_input(&quot;please input sth,q for exit.&quot;)
      if not sth:
      break
      else:
      print 'world'
      结果:
      [root@localhost 20171227]# python while.py
      hellow
      please input sth,q for exit.q
      world
      [root@localhost 20171227]#
      脚本6:
      #!/usr/bin/python
      sth=''
      while sth != 'q':
      print 'hellow'
      sth=raw_input(&quot;please input sth,q for exit.&quot;)
      if not sth:
      break
      if sth == 'quit':
      continue
      print 'continue'
      else:
      print 'world'
      
      结果:
      [root@localhost 20171227]# python while.py
      hellow
      please input sth,q for exit.ss
      continue
      hellow
      please input sth,q for exit.df
      continue
      hellow
      please input sth,q for exit.quit
      hellow
      please input sth,q for exit.q
      continue
      world

运维网声明 1、欢迎大家加入本站运维交流群:群②:261659950 群⑤:202807635 群⑦870801961 群⑧679858003
2、本站所有主题由该帖子作者发表,该帖子作者与运维网享有帖子相关版权
3、所有作品的著作权均归原作者享有,请您和我们一样尊重他人的著作权等合法权益。如果您对作品感到满意,请购买正版
4、禁止制作、复制、发布和传播具有反动、淫秽、色情、暴力、凶杀等内容的信息,一经发现立即删除。若您因此触犯法律,一切后果自负,我们对此不承担任何责任
5、所有资源均系网友上传或者通过网络收集,我们仅提供一个展示、介绍、观摩学习的平台,我们不对其内容的准确性、可靠性、正当性、安全性、合法性等负责,亦不承担任何法律责任
6、所有作品仅供您个人学习、研究或欣赏,不得用于商业或者其他用途,否则,一切后果均由您自己承担,我们对此不承担任何法律责任
7、如涉及侵犯版权等问题,请您及时通知我们,我们将立即采取措施予以解决
8、联系人Email:admin@iyunv.com 网址:www.yunweiku.com

所有资源均系网友上传或者通过网络收集,我们仅提供一个展示、介绍、观摩学习的平台,我们不对其承担任何法律责任,如涉及侵犯版权等问题,请您及时通知我们,我们将立即处理,联系人Email:kefu@iyunv.com,QQ:1061981298 本贴地址:https://www.yunweiku.com/thread-549856-1-1.html 上篇帖子: Python自动化开发学习4-2 下篇帖子: 用python实现冒泡,选择,快速排序
您需要登录后才可以回帖 登录 | 立即注册

本版积分规则

扫码加入运维网微信交流群X

扫码加入运维网微信交流群

扫描二维码加入运维网微信交流群,最新一手资源尽在官方微信交流群!快快加入我们吧...

扫描微信二维码查看详情

客服E-mail:kefu@iyunv.com 客服QQ:1061981298


QQ群⑦:运维网交流群⑦ QQ群⑧:运维网交流群⑧ k8s群:运维网kubernetes交流群


提醒:禁止发布任何违反国家法律、法规的言论与图片等内容;本站内容均来自个人观点与网络等信息,非本站认同之观点.


本站大部分资源是网友从网上搜集分享而来,其版权均归原作者及其网站所有,我们尊重他人的合法权益,如有内容侵犯您的合法权益,请及时与我们联系进行核实删除!



合作伙伴: 青云cloud

快速回复 返回顶部 返回列表