我正在编写一个接受用户输入的程序。
#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.")
只要用户输入有意义的数据,程序就能正常工作。
Please enter your age: 23
You are able to vote in the United States!
但如果用户输入无效数据,则失败:
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'
而不是崩溃,我希望程序再次要求输入。是这样的:
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!
我如何要求有效输入而不是崩溃或接受无效值(例如-1)?
好问题!您可以尝试以下代码。=)
这段代码使用ast.literal_eval()来查找输入的数据类型(age)。然后按照以下算法:
请用户输入年龄。
1.1. 如果age为float或int数据类型:
检查年龄>是否=18。如果age>=18,打印相应的输出并退出。
检查0<年龄<18。如果0<age<18,打印适当的输出并退出。
如果age<=0,请用户再次输入age的有效数字(即返回步骤1)。
1.2. 如果age不是float或int数据类型,则要求用户再次输入她/他的年龄(即返回第1步)。
这是代码。
from ast import literal_eval
''' This function is used to identify the data type of input data.'''
def input_type(input_data):
try:
return type(literal_eval(input_data))
except (ValueError, SyntaxError):
return str
flag = True
while(flag):
age = raw_input("Please enter your age: ")
if input_type(age)==float or input_type(age)==int:
if eval(age)>=18:
print("You are able to vote in the United States!")
flag = False
elif eval(age)>0 and eval(age)<18:
print("You are not able to vote in the United States.")
flag = False
else: print("Please enter a valid number as your age.")
else: print("Sorry, I didn't understand that.")
基于Daniel Q和Patrick Artner的优秀建议,
这里有一个更普遍的解决方案。
# Assuming Python3
import sys
class ValidationError(ValueError): # thanks Patrick Artner
pass
def validate_input(prompt, cast=str, cond=(lambda x: True), onerror=None):
if onerror==None: onerror = {}
while True:
try:
data = cast(input(prompt))
if not cond(data): raise ValidationError
return data
except tuple(onerror.keys()) as e: # thanks Daniel Q
print(onerror[type(e)], file=sys.stderr)
我选择了显式的if和raise语句,而不是assert,
因为断言检查可能被关闭,
而验证应始终开启以提供健壮性。
这可以用来获得不同种类的输入,
使用不同的验证条件。
例如:
# No validation, equivalent to simple input:
anystr = validate_input("Enter any string: ")
# Get a string containing only letters:
letters = validate_input("Enter letters: ",
cond=str.isalpha,
onerror={ValidationError: "Only letters, please!"})
# Get a float in [0, 100]:
percentage = validate_input("Percentage? ",
cast=float, cond=lambda x: 0.0<=x<=100.0,
onerror={ValidationError: "Must be between 0 and 100!",
ValueError: "Not a number!"})
或者,回答最初的问题:
age = validate_input("Please enter your age: ",
cast=int, cond=lambda a:0<=a<150,
onerror={ValidationError: "Enter a plausible age, please!",
ValueError: "Enter an integer, please!"})
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.")