新聞中心
1、冗余input語句的冗余使用,這種方法有效,但通常被認(rèn)為是糟糕的風(fēng)格。

10余年的麥積網(wǎng)站建設(shè)經(jīng)驗,針對設(shè)計、前端、開發(fā)、售后、文案、推廣等六對一服務(wù),響應(yīng)快,48小時及時工作處理。營銷型網(wǎng)站的優(yōu)勢是能夠根據(jù)用戶設(shè)備顯示端的尺寸不同,自動調(diào)整麥積建站的顯示方式,使網(wǎng)站能夠適用不同顯示終端,在瀏覽器中調(diào)整網(wǎng)站的寬度,無論在任何一種瀏覽器上瀏覽網(wǎng)站,都能展現(xiàn)優(yōu)雅布局與設(shè)計,從而大程度地提升瀏覽體驗。創(chuàng)新互聯(lián)從事“麥積網(wǎng)站設(shè)計”,“麥積網(wǎng)站推廣”以來,每個客戶項目都認(rèn)真落實執(zhí)行。
data = input("Please enter a loud message (must be all caps): ")
while not data.isupper():
print("Sorry, your response was not loud enough.")
data = input("Please enter a loud message (must be all caps): ")它最初可能看起來很有吸引力,因為它比while True方法短,但它違反了軟件開發(fā)的不要重復(fù)自己的原則。這會增加系統(tǒng)中出現(xiàn)錯誤的可能性。如果你想向移植到2.7通過改變input來raw_input,卻意外地只改變第一input上面?這SyntaxError只是等待發(fā)生。
2、遞歸會摧毀堆棧,用戶輸入無效數(shù)據(jù)的次數(shù)足夠多會出錯。
如果您剛剛了解了遞歸,您可能會想使用它get_non_negative_int來處理 while 循環(huán)。
def get_non_negative_int(prompt):
try:
value = int(input(prompt))
except ValueError:
print("Sorry, I didn't understand that.")
return get_non_negative_int(prompt)
if value < 0:
print("Sorry, your response must not be negative.")
return get_non_negative_int(prompt)
else:
return value這在大多數(shù)情況下似乎工作正常,但如果用戶輸入無效數(shù)據(jù)的次數(shù)足夠多,腳本將以RuntimeError: maximum recursion depth exceeded. 你可能認(rèn)為“沒有傻瓜會連續(xù)犯1000次錯誤”,但你低估了傻瓜的聰明才智!
以上就是python使用語句的常見陷阱,希望對大家有所幫助。 更多Python學(xué)習(xí)指路:創(chuàng)新互聯(lián)Python教程
本文教程操作環(huán)境:windows7系統(tǒng)、Python 3.9.1,DELL G3電腦。
當(dāng)前文章:創(chuàng)新互聯(lián)Python教程:python使用語句的常見陷阱
URL分享:http://m.fisionsoft.com.cn/article/dpgcgjg.html


咨詢
建站咨詢
