python有限循环_Python循环
# for循環(huán) #
## 實(shí)例1: ##
#基本語法
for i in range(100):
print(i)
#range(起始位,參數(shù),步長)
for j in range(1,100,2):#包括1,不包括100,顧頭不顧尾
print(j)
## 實(shí)例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("輸入錯(cuò)誤")
## 實(shí)例3: ##
#列表遍歷,enumerate(枚舉),遍歷加上序號,前面可以接受多個(gè)參數(shù):
a=['wuchao','jinxin','xiaohu','sanpang','ligang']
for i,v in enumerate(a,1):#enumerate(枚舉)遍歷時(shí)加上序號,可以自定義起始位置,for循環(huán)后面可以跟列表、字典、元組等,前面可以用2個(gè)參數(shù)接收
print(i,v)
# while循環(huán) #
## 實(shí)例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("輸入錯(cuò)誤")
conut+=1
## 實(shí)例2: ##
#加判斷,3次輸入結(jié)束,確認(rèn)是否繼續(xù)
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("輸入錯(cuò)誤")
count+=1
if count ==3:
keep_going = input("還想玩么?[y/n]")
if keep_going == 'y':
count = 0
else:
print('結(jié)束了')
# break #
跳出當(dāng)前循環(huán),循環(huán)結(jié)束,while和for循環(huán)后面都可以接else,如果程序正常結(jié)束,則走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 #
跳出本次循環(huán),開始下一次循環(huán)
總結(jié)
以上是生活随笔為你收集整理的python有限循环_Python循环的全部內(nèi)容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: python安装系统要求_python需
- 下一篇: C语言的typedef用法