当前位置:   article > 正文

Python之循环过程控制:break,continue,else_python 循环嵌套break continue

python 循环嵌套break continue
  • for循环break用法:

  1. while 1:
  2. addr=input("请输入IP地址:").strip()
  3. alist=addr.split(".")
  4. v=0
  5. if len(alist)==4:
  6. if(alist[0].isdigit() and
  7. alist[1].isdigit() and
  8. alist[2].isdigit() and
  9. alist[3].isdigit()):
  10. if((-1<int(alist[0]) and int(alist[0])<256) and
  11. (-1<int(alist[1]) and int(alist[1])<256) and
  12. (-1<int(alist[2]) and int(alist[2])<256) and
  13. (-1<int(alist[3]) and int(alist[3])<256) ):
  14. v=1
  15. if v:
  16. break
  17. print("输入格式不合法,请重新输入!")
  18. print("请输入您的IP地址",addr)

执行结果: 

  •  while循环的continue用法:

  1. i = 1
  2. while i < 20:
  3. i+= 1
  4. if i%2 > 0: # 非双数时跳过输出
  5. continue
  6. print (i) # 输出双数2、4、6、8、10

执行结果: 

  • while循环的else用法: 

  1. count=0
  2. while count<3:
  3. score=float(input("请输入分数:"))
  4. count=count+1
  5. if score<60:
  6. print("需要补考")
  7. break
  8. else:
  9. print("全部通过!")

执行结果: 

 

 

 

 

声明:本文内容由网友自发贡献,不代表【wpsshop博客】立场,版权归原作者所有,本站不承担相应法律责任。如您发现有侵权的内容,请联系我们。转载请注明出处:https://www.wpsshop.cn/w/很楠不爱3/article/detail/113610
推荐阅读
相关标签
  

闽ICP备14008679号