Python循环
Posted ccy7863257
tags:
篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了Python循环相关的知识,希望对你有一定的参考价值。
# 循环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循环的主要内容,如果未能解决你的问题,请参考以下文章