您好,登錄后才能下訂單哦!
一。while 循環(huán)
1.循環(huán):重復(fù)做某件事
2.語法
while 條件:
code1
3.結(jié)束while的方式:
1.條件不滿足,下次循環(huán)開始時(shí)判斷
2.break直接結(jié)束本層循環(huán)
4.while + continue
continue 之后的代碼不會(huì)運(yùn)行了,直接開始下次循環(huán)
n= 0
while n < 6:
if n == 4:
n+=1
continue
else:
print(n)
n+=1
5.while循環(huán)嵌套
while:
while:
while:
break
break
break
count = 1
tag = True
while tag:
name = input("name: ")
passwd = input("passwd ")
if count == 3:
print("too many")
break
if name == "chad" and passwd == "123":
print("successfull")
while tag:
print("""
1
2
3
""")
cho = input("choice:")
if cho == "1":
print(1)
elif cho == "2":
print(2)
else:
print(3)
tag = False
else:
print("error")
count +=1
6. while + else
如果while循環(huán)沒有被break打斷,才會(huì)執(zhí)行
循環(huán)要正常結(jié)束
count = 0
while count < 3:
print(count)
count += 1
else:
print("run")
二。for 循環(huán)
循環(huán)取值簡潔
for+brek
for+continue
for+else
dic = {"name":"sdfa","age":23}
for i in dic:
print(i,dic[i])
返回字符串
dic = {"name":"sdfa","age":23}
for i,r in dic.items():
print(i,r)
以字符串返回key和value
dic = {"name":"sdfa","age":23}
for i in dic.items():
print(i)
以元組返回key和value,一對(duì)key,value是一個(gè)元組
range(起始,結(jié)束,步長)
取頭不取尾
免責(zé)聲明:本站發(fā)布的內(nèi)容(圖片、視頻和文字)以原創(chuàng)、轉(zhuǎn)載和分享為主,文章觀點(diǎn)不代表本網(wǎng)站立場,如果涉及侵權(quán)請(qǐng)聯(lián)系站長郵箱:is@yisu.com進(jìn)行舉報(bào),并提供相關(guān)證據(jù),一經(jīng)查實(shí),將立刻刪除涉嫌侵權(quán)內(nèi)容。