(20)循环语句

* while 循环
   # while expression:
             statement(s)
            
    猜数字
    #!/usr/bin/env python
    # -*- coding: utf-8 -*-

    import random
    number = random.randint(1,101)
    guess=0
    while True:
        num_input=raw_input("please input one integer that is in 1 to 100:")
        guess +=1 #统计猜的次数

        if not num_input.isdigit():
            print "please input integer."
        elif int(num_input)<0 or int(num_input)>100:
            print "The number should in 0 to 100."
        else:
            if number == int(num_input):
                print "Ok,you are good. It is only %d, then you success."%guess
                break #猜对了就跳出循环
            elif number>int(num_input):
                print "Your number is more less."
            elif number<int(num_input):
                print "Your number is bigger"
            else:
                print "something is wrong."
           
    root@erp-yejian-dev:/var/www/erp-yejian# python test.py
    please input one integer that is in 1 to 100:50
    Your number is more less.
    please input one integer that is in 1 to 100:75
    Your number is bigger
    please input one integer that is in 1 to 100:65
    Your number is bigger
    please input one integer that is in 1 to 100:55
    Your number is more less.
    please input one integer that is in 1 to 100:60
    Your number is more less.
    please input one integer that is in 1 to 100:63
    Your number is more less.
    please input one integer that is in 1 to 100:64
    Ok,you are good. It is only 7, then you success.
   
  # break 和 continue
   break:跳出循环
   continue:跳过本次循环进入下一次循环
   这两个都是在循环体中控制循环流程
  
  # while ... else...
   当运行到else时,就出了循环体
    #!/usr/bin/env python
    # -*- coding: utf-8 -*-

    count=0
    while count<3:
        print  count," is less than 3"
        count+=1
    else:
        print count," is not less than 3"
       
    root@erp-yejian-dev:/var/www/erp-yejian# python test.py
    0  is less than 3
    1  is less than 3
    2  is less than 3
    3  is not less than 3   
   
* 强大的for
    >>> mystr="hello"
    >>> for i in mystr:
    ...     print i,
    ...    
    h e l l o
    一个字母一个字母打印出来
   
    >>> mylist=[11,'good',22]
    >>> for i in mylist:
    ...     print i,
    ...    
    11 good 22
    遍历列表
   
    >>> mytuple=(1,'ss',2)
    >>> for i in mytuple:
    ...     print i,
    ...    
    1 ss 2
    遍历元组
   
    >>> mydict={'first':"good","second":"better"}
    >>> for i in mydict.values():
    ...     print i,
    ...    
    better good
    遍历字典
   
    >>> myset={'good','better'}
    >>> for i in myset:
    ...     print i,
    ...    
    better good
    遍历集合
   
    >>> for i in range(2,8):
    ...     print i,
    ...    
    2 3 4 5 6 7
    遍历range产生的列表
   
    >>> [i for i in range(3,9) if i%2==0]
    Out[2]: [4, 6, 8]
    列表解析中遍历
   
    >>> str1
    Out[16]: 'hello'
    >>> str2
    Out[17]: 'world'
    >>> for x,y in zip(str1,str2):
    ...     print x,y
    ...    
    h w
    e o
    l r
    l l
    o d
    和zip函数使用
   
* 迭代
    # iter
    >>> mylist
    Out[42]: ['name', 88]
    >>> list_iter=iter(mylist)
    >>> while True:
    ...     print list_iter.next()
    ...    
    name
    88
    一个一个的输出

原文地址:https://www.cnblogs.com/toby2chen/p/5239564.html