python編程 python中什么時(shí)候使用while True什么使用while 條件?
python中什么時(shí)候使用while True什么使用while 條件?while True的意思是死循環(huán),即一直循環(huán)執(zhí)行下去。這個(gè)極少場(chǎng)景會(huì)用到,比較危險(xiǎn),使用的時(shí)候要千萬(wàn)注意。while 條件就是
python中什么時(shí)候使用while True什么使用while 條件?
while True的意思是死循環(huán),即一直循環(huán)執(zhí)行下去。
這個(gè)極少場(chǎng)景會(huì)用到,比較危險(xiǎn),使用的時(shí)候要千萬(wàn)注意。
while 條件就是滿足這個(gè)條件才會(huì)執(zhí)行
python兩個(gè)while循環(huán)?
def trim(s):
if len(s)==0:
return s
else:
while s[0:1]==" ":
if s[0:1]!=" ":
break
s=s[1:]
print(s)
#return s 這里不能加一個(gè)return 如果加了,會(huì)把第一個(gè)while的結(jié)果返回
# 這個(gè)結(jié)果就不能繼續(xù)運(yùn)行第二個(gè)while循環(huán)了
while s[-1:]==" ":
if s[-1:]!=" ":
break
s=s[:-1]
return s
return s
# 測(cè)試:
if trim("hello ") != "hello":
print("測(cè)試失敗1!")
elif trim(" hello") != "hello":
print("測(cè)試失敗2!")
elif trim(" hello ") != "hello":
print("測(cè)試失敗3!")
elif trim(" hello world ") != "hello world":
print("測(cè)試失敗4!")
elif trim("") != "":
print("測(cè)試失敗5!")
elif trim(" ") != "":
print("測(cè)試失敗6!")
else:
print("測(cè)試成功!")