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

  • 相关阅读:
    PHP 输出true false
    code::blocks 注释快捷键
    GDAL 网址
    wine qq2011安装
    C++ ACM解题
    C++内存分配秘籍—new,malloc,GlobalAlloc详解(Zhuan)
    grub4dos初级教程-入门篇(Z)
    GDAL 编译(转)
    ubuntu双系统安装
    shapfile格式说明(转)
  • 原文地址:https://www.cnblogs.com/solozhou/p/6406399.html
Copyright © 2011-2022 走看看