第七色在线视频,2021少妇久久久久久久久久,亚洲欧洲精品成人久久av18,亚洲国产精品特色大片观看完整版,孙宇晨将参加特朗普的晚宴

為了賬號(hào)安全,請(qǐng)及時(shí)綁定郵箱和手機(jī)立即綁定
已解決430363個(gè)問題,去搜搜看,總會(huì)有你想問的

詢問用戶輸入,直到他們給出有效的響應(yīng)

詢問用戶輸入,直到他們給出有效的響應(yīng)

翻閱古今 2019-05-20 16:10:06
我正在編寫一個(gè)必須接受用戶輸入的程序。#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.")如果用戶輸入合理數(shù)據(jù),這將按預(yù)期工作。C:\Python\Projects> canyouvote.py Please enter your age: 23 You are able to vote in the United States!但如果他們犯了錯(cuò)誤,那就崩潰了: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!我怎么能做到這一點(diǎn)?如果我還想拒絕像這樣的上下文中-1的有效int但無意義的值,該怎么辦?
查看完整描述

4 回答

?
動(dòng)漫人物

TA貢獻(xiàn)1815條經(jīng)驗(yàn) 獲得超10個(gè)贊

雖然接受的答案是驚人的。我還想分享這個(gè)問題的快速入侵。(這也解決了負(fù)面年齡問題。)

f=lambda age: (age.isdigit() and ((int(age)>=18  and "Can vote" ) or "Cannot vote")) or \
f(input("invalid input. Try again\nPlease enter your age: "))print(f(input("Please enter your age: ")))

PS此代碼適用于python 3.x.


查看完整回答
反對(duì) 回復(fù) 2019-05-20
  • 4 回答
  • 0 關(guān)注
  • 650 瀏覽
慕課專欄
更多

添加回答

舉報(bào)

0/150
提交
取消
微信客服

購課補(bǔ)貼
聯(lián)系客服咨詢優(yōu)惠詳情

幫助反饋 APP下載

慕課網(wǎng)APP
您的移動(dòng)學(xué)習(xí)伙伴

公眾號(hào)

掃描二維碼
關(guān)注慕課網(wǎng)微信公眾號(hào)