1 什么是循環(huán)
循環(huán)就是一個重復(fù)的過程
2 為何要有循環(huán)
人可以重復(fù)的去做某一件事
程序中必須有一種機(jī)制能夠控制計算機(jī)像人一樣重復(fù)地去做某一件事
3 如何用循環(huán)
# 語法
# while 條件:
# code1
# code2
# code3
# ...
一、while 循環(huán)
while True: inp_user=input('please input your username: ') inp_pwd=input('please input your password: ') if inp_user == "user" and inp_pwd == "123": print('login successfull') else: print('user or password err')# while + break: break代表結(jié)束本層循環(huán)
while True: inp_user=input('please input your username: ') inp_pwd=input('please input your password: ') if inp_user == "user" and inp_pwd == "123": print('login successfull') break else: print('user or password err')# while+continue:continue代表結(jié)束本次循環(huán)(本次循環(huán)continue之后的代碼不在運(yùn)行),直接進(jìn)入下一次循環(huán)
# 強(qiáng)調(diào):continue一定不要作為循環(huán)體的最后一步代碼
#while循環(huán)的嵌套
user_from_db='egon' pwd_from_db='123' while True: inp_user=input('please input your username: ') inp_pwd=input('please input your password: ') if inp_user == user_from_db and inp_pwd == pwd_from_db: print('login successfull') while True: cmd=input('>>>: ') # cmd='quit' if cmd == 'quit': break print('%s run......' %cmd) break else: print('user or password err')給tag打標(biāo):
user_from_db='egon' pwd_from_db='123' tag=True while tag: inp_user=input('please input your username: ') inp_pwd=input('please input your password: ') if inp_user == user_from_db and inp_pwd == pwd_from_db: print('login successfull') while tag: cmd=input('>>>: ') # cmd='quit' if cmd == 'quit': tag=False break print('%s run......' %cmd) else: print('user or password err')# while + else
# else的代碼會在while循環(huán)沒有break打斷的情況下最后運(yùn)行
密碼數(shù)錯3次程序退出
user_from_db='egon' pwd_from_db='123' count=0 tag=True while tag: if count == 3: print('輸錯次數(shù)過多') break inp_user=input('please input your username: ') inp_pwd=input('please input your password: ') if inp_user == user_from_db and inp_pwd == pwd_from_db: print('login successfull') while tag: cmd=input('>>>: ') # cmd='quit' if cmd == 'quit': tag=False break print('%s run......' %cmd) else: print('user or password err') count+=1 #count=3 # 輸錯3次二、流程控制之for循環(huán)
names=['egon','alex_dsb','lxx_sb','yxx_dsb'] i=0 while i < len(names): #4 < 4 print(names[i]) i+=1# for循環(huán):可以不依賴索引而取指
names=['egon','alex_dsb','lxx_sb','yxx_dsb'] for item in names: #item='lxx_sb' print(item)dic={'x':1,'y':2,'z':3} for k in dic: #k='x' print(k,dic[k])# for vs while
# for可以不依賴于索引取指,是一種通用的循環(huán)取指方式
# for的循環(huán)次數(shù)是由被循環(huán)對象包含值的個數(shù)決定的,而while的循環(huán)次數(shù)是由條件決定的
# for+break
# for+continue
# for+else
for i in range(10): if i == 4: break print(i) else: print('====>')練習(xí)
'''
1*1=1
2*1=2 2*2=4
3*1=3 3*2=6 3*3=9
...
9*1=9.................9*9=81
'''
''' max_level=5
* current_level=1 空格數(shù)=4 星號=1
*** current_level=2 空格數(shù)=3 星號=3
***** current_level=3 空格數(shù)=2 星號=5
******* current_level=4 空格數(shù)=1 星號=7
********* current_level=5 空格數(shù)=0 星號=9
'''
另外有需要云服務(wù)器可以了解下創(chuàng)新互聯(lián)cdcxhl.cn,海內(nèi)外云服務(wù)器15元起步,三天無理由+7*72小時售后在線,公司持有idc許可證,提供“云服務(wù)器、裸金屬服務(wù)器、高防服務(wù)器、香港服務(wù)器、美國服務(wù)器、虛擬主機(jī)、免備案服務(wù)器”等云主機(jī)租用服務(wù)以及企業(yè)上云的綜合解決方案,具有“安全穩(wěn)定、簡單易用、服務(wù)可用性高、性價比高”等特點與優(yōu)勢,專為企業(yè)上云打造定制,能夠滿足用戶豐富、多元化的應(yīng)用場景需求。
網(wǎng)站題目:二、流程控制之while循環(huán)與for循環(huán)-創(chuàng)新互聯(lián)
文章出自:http://www.chinadenli.net/article38/dcsepp.html
成都網(wǎng)站建設(shè)公司_創(chuàng)新互聯(lián),為您提供外貿(mào)網(wǎng)站建設(shè)、建站公司、小程序開發(fā)、網(wǎng)站導(dǎo)航、網(wǎng)站維護(hù)、ChatGPT
聲明:本網(wǎng)站發(fā)布的內(nèi)容(圖片、視頻和文字)以用戶投稿、用戶轉(zhuǎn)載內(nèi)容為主,如果涉及侵權(quán)請盡快告知,我們將會在第一時間刪除。文章觀點不代表本網(wǎng)站立場,如需處理請聯(lián)系客服。電話:028-86922220;郵箱:631063699@qq.com。內(nèi)容未經(jīng)允許不得轉(zhuǎn)載,或轉(zhuǎn)載時需注明來源: 創(chuàng)新互聯(lián)
猜你還喜歡下面的內(nèi)容