2020-07-09 流程控制

? ? ? ? ? ? ? 流程控制之while循環(huán)

語法:

while?條件:

? ? 代碼1

? ? 代碼2

? ? 代碼3

基本使用1:

print('start.....')

while 10<.:

? ? print('hello')

? ???print('hello')

? ???print('hello')

print('end..................')? ? ? ? ? ? ? 死循環(huán)? end打印不出來

基本使用2:

count=0

while count<6:

? ? print(count)

? ? count+=1

print('end.............')? ? ? ? ? ?打印出來0-5?最后打印end.............'

結(jié)束while循環(huán)的三種方式:

1.把條件改為False

db_name ="egon"

db_pwd ="123"

tag =True

while tag:

????????????inp_name =input("請輸入您的用戶名: ")

????????????inp_pwd =input("請輸入您的密碼: ")

????????????if inp_name == db_nameand inp_pwd == db_pwd:

????????????????????????print("用戶登錄成功")

????????????????????????tag =False

? ? ? ? ? ? ? else:

????????????????????????print("用戶賬號或密碼錯誤")

????????????????print('其他。。。。。。。。。')

2.break:直接終止本層循環(huán)

name='egon'

pwd='123'

while? True:

? ? ? ? ? ? inp_name=input('請輸入賬號:').strip

? ??????????inp_pwd=input('請輸入密碼:').strip

? ? ? ? ? ? if? inp_name==name and inp_pwd==pwd:

? ? ? ? ? ? ? ? ? ? print('登錄成功')

? ? ? ? ? ? ? ? ? ? break

? ? ? ? ? ? ? ?else:

? ? ? ? ? ? ? ? ? ? ? ? print('賬號密碼錯誤')

? ? ? ? ? ? ? ? print('只要輸入錯誤,每次都會打印一遍')

(想要結(jié)束多少層就要打幾個(gè)break)

while? True:

? ??????while? True:

? ??????????????while? True:

? ? ? ? ? ? ? ? ? ? ? ? break

? ???????????????break

? ? ? ? ? ??break


tag=True

while tag:

? ? ? ? while tag:

? ? ? ? ? ? ? ? while tag:

? ? ? ? ? ? ? ? ? ? ? ? tag=False? ?(可以一次性結(jié)束全部循環(huán))



3.while +?continue:?終止本次循環(huán)? ?(不要在continue之后寫同級代碼)

count=0

while count <6:

? ? ? ? if count ==3 or? count==4:

? ? ? ? ? ? ? ? ? ? count+=1

? ? ? ? ? ? ? ? ? ? continue

? ? ? ? ? ? ? ? ? ? 注意? count+=1?不能寫在continue之后

? ? ? ? ? ? print(count)

? ? ? ? ? ? coun+=1

4.while +? else? ? (在while程序正常結(jié)束后?才運(yùn)行else之后的代碼)

count=0

while count <6

? ??????????????print(count)

? ??????????????if count == 3:

? ??????????????????????break

? ??????????????count+=1

else:

? ? ?? print('會在while循環(huán)正常死亡之后運(yùn)行')




? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? for?循環(huán)? ??

特點(diǎn)及用途:1.主要用于循環(huán)取值,列如列表?字典?字符串

? ? ? ? ? ? ? ? ? ? ? ?2.for循環(huán)的次數(shù)取決于值的個(gè)數(shù)

? ? ? ? ? ? ? ? ? ? ? ? ? ? while循環(huán)循環(huán)的次數(shù)主要取決于條件什么時(shí)候變?yōu)镕alse或者什么時(shí)候執(zhí)行break

一:基本使用

l=[111,222,333,444,555]? ? #用while循環(huán)? 復(fù)雜

i=0

while i < len(l):

? ? print(l[i])

? ? i+=1

for x in l:? ? ? ? ? ? ? ? 用for循環(huán)? 簡單

? ? print(x)

d={'k1':111,'k2':222,'k3':333}? ? #for循環(huán)? 循環(huán)字典

for k in d :? ? ? for循環(huán) 循環(huán)出來的是key值

? ? print(k,d[k])

msg='hello word'? ? ? ? for循環(huán)? 循環(huán)字符串

for x in msg:

? ? print(x,end='')? ? ? 如果想打印到一行需要在后加上end=''

l=[['aaa',111],['bbb',222],['ccc',333]]? ? #列表套列表的解壓賦值

for x,y in l:? ? #x,y=['aaa',111]

? ? print(x,y)

二:for+break

for x in [111,222,333,444,555]:

? ? if x==333:

? ? ? ? break

? ? print(x)

三:for+continue

for x in? [111,222,333,444,555]:

? ? if x==333:

? ? ? ? continue

? ? print(x)

四:for+else? ? ? ? 只有在for循環(huán)是自然結(jié)束的時(shí)候else之后的代碼才開始運(yùn)行

for x in? [111,222,333,444,555]:

? ? if x==333:

? ? ? ? break

? ? print(x)

else:

? ? print('===========>')

? ??

?著作權(quán)歸作者所有,轉(zhuǎn)載或內(nèi)容合作請聯(lián)系作者
【社區(qū)內(nèi)容提示】社區(qū)部分內(nèi)容疑似由AI輔助生成,瀏覽時(shí)請結(jié)合常識與多方信息審慎甄別。
平臺聲明:文章內(nèi)容(如有圖片或視頻亦包括在內(nèi))由作者上傳并發(fā)布,文章內(nèi)容僅代表作者本人觀點(diǎn),簡書系信息發(fā)布平臺,僅提供信息存儲服務(wù)。

友情鏈接更多精彩內(nèi)容