python有限循环_Python循环
# 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有限循环_Python循环的全部內容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: python安装系统要求_python需
- 下一篇: C语言的typedef用法