zoukankan      html  css  js  c++  java
  • Python学习笔记(七)-流程控制(while循环)

    While循环:
    Python流程控制中的while循环,直到表达式变为假才会结束。表达的是一个逻辑表达式,必须返回一个true或false
    语法如下:
    while expression:
      statement(s) #请注意while循环也是要遵循代码缩进原则的

    while后接true条件:
      print '' #始终为true,会进入死循环,一直print打印。。,没有实际意义。。

    那么,我们在设计Python程序时就要设置一个条件,使程序终止,而不是直接Ctrl+C去终止程序。。
    下面看实例:

     1 #coding:utf-8
     2 import time
     3 '''
     4 while True:
     5     print "Enjoy Youself!" #会进入死循环,只有手动按Ctrl+C才能结束
     6     x = raw_input("Please input somting  for quit the program !")
     7     if x =="q":#添加此条件后,只有用户输入的值为q时,程序才会结束
     8         break
     9 '''
    10 
    11 x=""#字符串
    12 a = str() #字符串
    13 b = int()
    14 c = float()
    15 d = []
    16 e = {}
    17 print type(x),id(x)
    18 print type(a),id(a)
    19 print type(b),id(b)
    20 print type(c),id(c)
    21 print type(d),id(d)
    22 print type(e),id(e)
    23 
    24 time.sleep(3)
    25 
    26 
    27 while x!="q":#x=q时为假,x!=q时就为真
    28     print "Enjoy Youself!"
    29     x = raw_input("Please input somting  for quit the program:")
    30     if not x :#x为空(直接按Enter键程序也会结束),其实也是False
    31         break
    32     if x == "c":
    33         continue
    34     print "one more time*****************"
    35 else:#当条件失败如输入q),执行else
    36     print "End......."
  • 相关阅读:
    洛谷 P1244 青蛙过河
    洛谷 P1004 方格取数
    洛谷 CF894A QAQ
    【题解】洛谷 P5506 封锁
    洛谷 P3884 [JLOI2009]二叉树问题
    Bzoj4894 天赋
    Bzoj4893 项链分赃
    Bzoj3583 杰杰的女性朋友
    POJ3233 Matrix Power Series
    UOJ#204 【APIO2016】Boat
  • 原文地址:https://www.cnblogs.com/helloworldcc/p/7351726.html
Copyright © 2011-2022 走看看