如何解決python面對(duì)用戶(hù)無(wú)意義輸入的問(wèn)題

這篇文章主要介紹如何解決python面對(duì)用戶(hù)無(wú)意義輸入的問(wèn)題,文中介紹的非常詳細(xì),具有一定的參考價(jià)值,感興趣的小伙伴們一定要看完!

創(chuàng)新互聯(lián)公司堅(jiān)持“要么做到,要么別承諾”的工作理念,服務(wù)領(lǐng)域包括:成都網(wǎng)站設(shè)計(jì)、網(wǎng)站建設(shè)、企業(yè)官網(wǎng)、英文網(wǎng)站、手機(jī)端網(wǎng)站、網(wǎng)站推廣等服務(wù),滿(mǎn)足客戶(hù)于互聯(lián)網(wǎng)時(shí)代的汝城網(wǎng)站設(shè)計(jì)、移動(dòng)媒體設(shè)計(jì)的需求,幫助企業(yè)找到有效的互聯(lián)網(wǎng)解決方案。努力成為您成熟可靠的網(wǎng)絡(luò)建設(shè)合作伙伴!

問(wèn)題

正在編寫(xiě)一個(gè)接受用戶(hù)輸入的程序。

#note: Python 2.7 users should use `raw_input`, the equivalent of 3.X's `input`
age = int(input("Please enter your age: "))
if age >= 18:
    print("You are able to vote in the United States!")
else:
    print("You are not able to vote in the United States.")

只要用戶(hù)輸入有意義的數(shù)據(jù),程序就會(huì)按預(yù)期工作。

C:\Python\Projects> canyouvote.py
Please enter your age: 23
You are able to vote in the United States!

但如果用戶(hù)輸入無(wú)效數(shù)據(jù),它就會(huì)失?。?/p>

C:\Python\Projects> canyouvote.py
Please enter your age: dickety six
Traceback (most recent call last):
  File "canyouvote.py", line 1, in <module>
    age = int(input("Please enter your age: "))
ValueError: invalid literal for int() with base 10: 'dickety six'

而不是崩潰,我希望程序再次要求輸入。像這樣:

C:\Python\Projects> canyouvote.py
Please enter your age: dickety six
Sorry, I didn't understand that.
Please enter your age: 26
You are able to vote in the United States!

當(dāng)輸入無(wú)意義的數(shù)據(jù)時(shí),如何讓程序要求有效輸入而不是崩潰?

我如何拒絕像 那樣的值-1,int在這種情況下這是一個(gè)有效但無(wú)意義的值?

解決方法

完成此操作的最簡(jiǎn)單方法是將input方法放入 while 循環(huán)中。使用continue時(shí),你會(huì)得到錯(cuò)誤的輸入,并break退出循環(huán)。

當(dāng)您的輸入可能引發(fā)異常時(shí)

使用try和except檢測(cè)用戶(hù)何時(shí)輸入無(wú)法解析的數(shù)據(jù)。

while True:
    try:
        # Note: Python 2.x users should use raw_input, the equivalent of 3.x's input
        age = int(input("Please enter your age: "))
    except ValueError:
        print("Sorry, I didn't understand that.")
        #better try again... Return to the start of the loop
        continue
    else:
        #age was successfully parsed!
        #we're ready to exit the loop.
        break
if age >= 18:
    print("You are able to vote in the United States!")
else:
print("You are not able to vote in the United States.")

實(shí)現(xiàn)你自己的驗(yàn)證規(guī)則

如果要拒絕 Python 可以成功解析的值,可以添加自己的驗(yàn)證邏輯。

while True:
    data = input("Please enter a loud message (must be all caps): ")
    if not data.isupper():
        print("Sorry, your response was not loud enough.")
        continue
    else:
        #we're happy with the value given.
        #we're ready to exit the loop.
        break
 
while True:
    data = input("Pick an answer from A to D:")
    if data.lower() not in ('a', 'b', 'c', 'd'):
        print("Not an appropriate choice.")
    else:
        Break

結(jié)合異常處理和自定義驗(yàn)證

以上兩種技術(shù)都可以組合成一個(gè)循環(huán)。

while True:
    try:
        age = int(input("Please enter your age: "))
    except ValueError:
        print("Sorry, I didn't understand that.")
        continue
 
    if age < 0:
        print("Sorry, your response must not be negative.")
        continue
    else:
        #age was successfully parsed, and we're happy with its value.
        #we're ready to exit the loop.
        break
if age >= 18:
    print("You are able to vote in the United States!")
else:
    print("You are not able to vote in the United States.")

以上是“如何解決python面對(duì)用戶(hù)無(wú)意義輸入的問(wèn)題”這篇文章的所有內(nèi)容,感謝各位的閱讀!希望分享的內(nèi)容對(duì)大家有幫助,更多相關(guān)知識(shí),歡迎關(guān)注創(chuàng)新互聯(lián)行業(yè)資訊頻道!

分享文章:如何解決python面對(duì)用戶(hù)無(wú)意義輸入的問(wèn)題
網(wǎng)頁(yè)地址:http://bm7419.com/article18/psdedp.html

成都網(wǎng)站建設(shè)公司_創(chuàng)新互聯(lián),為您提供面包屑導(dǎo)航網(wǎng)站內(nèi)鏈、定制網(wǎng)站、網(wǎng)站排名ChatGPT、標(biāo)簽優(yōu)化

廣告

聲明:本網(wǎng)站發(fā)布的內(nèi)容(圖片、視頻和文字)以用戶(hù)投稿、用戶(hù)轉(zhuǎn)載內(nèi)容為主,如果涉及侵權(quán)請(qǐng)盡快告知,我們將會(huì)在第一時(shí)間刪除。文章觀點(diǎn)不代表本網(wǎng)站立場(chǎng),如需處理請(qǐng)聯(lián)系客服。電話:028-86922220;郵箱:631063699@qq.com。內(nèi)容未經(jīng)允許不得轉(zhuǎn)載,或轉(zhuǎn)載時(shí)需注明來(lái)源: 創(chuàng)新互聯(lián)

成都網(wǎng)站建設(shè)公司