码迷,mamicode.com
首页 > 编程语言 > 详细

Python循环

时间:2019-11-18 18:52:18      阅读:74      评论:0      收藏:0      [点我收藏+]

标签:接收   span   错误   输入   font   python   bsp   loop   name   

# 循环loop #

    有限循环 ,次数限制
    无限循环=死循环
    continue 结束本次循环,继续下一次循环
    break 跳出整个当前的循环

 

# for循环 #

## 实例1: ##
    #基本语法 
    for i in range(100):
        print(i)
    #range(起始位,参数,步长)
    for j in range(1,100,2):#包括1,不包括100,顾头不顾尾
        print(j)


## 实例2: ## #用户只能输入3次: name = ccy passwd = 123456 for i in range(3): username = input("Username:") password = input("Password:") if name==username and passwd==password: print("welcome %s login" %(username)) break else: print("输入错误") ## 实例3: ## #列表遍历,enumerate(枚举),遍历加上序号,前面可以接受多个参数: a=[wuchao,jinxin,xiaohu,sanpang,ligang] for i,v in enumerate(a,1):#enumerate(枚举)遍历时加上序号,可以自定义起始位置,for循环后面可以跟列表、字典、元组等,前面可以用2个参数接收 print(i,v)

 



# while循环 #

## 实例1: ##
    #用户只能输入3次:
    name = ccy
    passwd = 123456
    conut = 0
    while conut<3:
        username = input("Username:")
        password = input("Password:")
        if  name==username and passwd==password:
            print("welcome %s login" %(username))
            break
        else:
            print("输入错误")
        conut+=1

## 实例2: ## #加判断,3次输入结束,确认是否继续 name = ccy passwd = 123456 count = 0 while count<3: username = input("Username:") password = input("Password:") if name==username and passwd==password: print("welcome %s login" %(username)) break else: print("输入错误") count+=1 if count ==3: keep_going = input("还想玩么?[y/n]") if keep_going == y: count = 0 else: print(结束了) # break # 跳出当前循环,循环结束,while和for循环后面都可以接else,如果程序正常结束,则走else,如果被break中断,则不走else #while......else..... while count <1: print(count) break else: print("111") #for......else..... for i in range(3): print(count) break else: print("111") # continue # 跳出本次循环,开始下一次循环

 

 

 

 

 

# for循环 #
## 实例1: ##
    #基本语法 
    for i in range(100):
        print(i)
    #range(起始位,参数,步长)
    for j in range(1,100,2):#包括1,不包括100,顾头不顾尾
        print(j)

## 实例2: ##
    #用户只能输入3次:
    name = ‘ccy‘
    passwd = ‘123456‘

    for i in range(3):
        username = input("Username:")
        password = input("Password:")
        if  name==username and passwd==password:
            print("welcome %s login" %(username))
            break
        else:
            print("输入错误")
## 实例3: ##
    #列表遍历,enumerate(枚举),遍历加上序号,前面可以接受多个参数:
    a=[‘wuchao‘,‘jinxin‘,‘xiaohu‘,‘sanpang‘,‘ligang‘]
    for i,v in enumerate(a,1):#enumerate(枚举)遍历时加上序号,可以自定义起始位置,for循环后面可以跟列表、字典、元组等,前面可以用2个参数接收
    print(i,v)

# while循环 #
## 实例1: ##
    #用户只能输入3次:
    name = ‘ccy‘
    passwd = ‘123456‘
    conut = 0
    while conut<3:
        username = input("Username:")
        password = input("Password:")
        if  name==username and passwd==password:
            print("welcome %s login" %(username))
            break
        else:
            print("输入错误")
        conut+=1

## 实例2: ##
    #加判断,3次输入结束,确认是否继续
    name = ‘ccy‘
    passwd = ‘123456‘
    count = 0
    while count<3:
        username = input("Username:")
        password = input("Password:")
        if  name==username and passwd==password:
            print("welcome %s login" %(username))
            break
        else:
            print("输入错误")
        count+=1
        if count ==3:
            keep_going = input("还想玩么?[y/n]")
            if keep_going == ‘y‘:
                count = 0
    else:
        print(‘结束了‘)


# break #
    跳出当前循环,循环结束,while和for循环后面都可以接else,如果程序正常结束,则走else,如果被break中断,则不走else
    #while......else.....
    while count <1:
        print(count)
        break
    else:
        print("111")
    
    #for......else.....
    for i in range(3):
        print(count)
        break
    else:
        print("111")


# continue #
    跳出本次循环,开始下一次循环

Python循环

标签:接收   span   错误   输入   font   python   bsp   loop   name   

原文地址:https://www.cnblogs.com/ccy7863257/p/11883662.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!