zoukankan      html  css  js  c++  java
  • python学习随笔之流程控制---while

    死循环:

    1 #!/usr/bin/python
    2 while 1:
    3     print "hello"

    设置条件跳出循环:

    1 #!/usr/bin/python
    2 x = ""              #定义x为空字符,如果不定义会出现语法错误
    3 while x != "q":#如果x不等于q为真,则循环,如果x不等于q为假,即x=q为真,则跳出循环,执行"ending" 
    4     print "hello"
    5     x = raw_input("please input something ,q for quit: ")
    6 else:
    7     print "ending"

    上述方法使用q退出,也可以使用回车退出

    1 #!/usr/bin/python
    2 x = ""             
    3 while x != "q":4     print "hello"
    5     x = raw_input("please input something ,q for quit: ")
    6     if not x:
    7         break
    8 if x == c:
    9 continue
    10 print "#"*50
    11 else: 12 print "ending"

    跳出循环则不显示ending

    continue用法与for一样:

     1 #!/usr/bin/python
     2 x = ""
     3 while x != "q":
     4     print "hello"
     5     x = raw_input("please input something ,q for quit: ")
     6     if not x:
     7         break
     8     if x == "c":
     9        continue
    10     print "#"*50
    11 else:
    12     print "ending"

    当输入a时:

    hello

    ###################

    当输入c时:

    hello

    无#显示,因为continue

  • 相关阅读:
    深度学习中的基本概念——梯度相关
    模型训练中涉及到的技巧
    ubuntu swapfile
    ubuntu install opencv
    Jetson TX1 安装ROS操作系统
    Redis介绍以及安装(Linux)
    C# url信息获取
    贝茨视觉训练法
    PHP设计模式浅析
    MySQL相关知识
  • 原文地址:https://www.cnblogs.com/solozhou/p/6406399.html
Copyright © 2011-2022 走看看