Python 3-如何验证多个用户输入



我创建了一个函数,它接受用户的输入并填充列表[Persones]。在这个函数中,我想进行一些验证,以保护用户输入。

即请只提供数字。请只提供字符串。

我可以验证用户"Name",但如何对第二个变量("LastName"(进行验证。

persons = [
{
"id": 1001,
"name": "xxxx",
"lastname": "xxx",
"fathers_name": "xxxx",
"age": 34,
"class": 1,
"id_number": "xxx"
},
{
"id": 1002,
"name": "xxx",
"lastname": "xxx",
"fathers_name": "xxxxx",
"age": 30,
"class": 3,
"id_number": "xxxxxx"
},
{
"id": 1003,
"name": "xxxx",
"lastname": "xx",
"fathers_name": "xxxx",
"age": 16,
"class": 4,
"id_number": "xxxx"
},]
def create():
person = {}
while True:
name = input("Give Name: ").capitalize()
person["name"] = name
if not name.isalpha():
print("Please give a string...")
continue
else:
break
while True:
lastname = input("Give LastName: ").capitalize()
person["lastname"] = lastname
if not lastname.isalpha():
print("Please give a string...")
continue
else:
break
print(person)

创建((

您可以预先定义某种验证模式:

person_fields = {
"name": {
"pretty": "first name",
"validation": str.isalpha
},
"lastname": {
"pretty": "last name",
"validation": str.isalpha
},
"fathers_name": {
"pretty": "father's name",
"validation": str.isalpha
},
"age": {
"pretty": "age",
"validation": str.isdigit
},
"class": {
"pretty": "class",
"validation": str.isdigit
}
}
for field, info in person_fields.items():
while True:
user_input = input(f"Please enter your {info['pretty']}: ")
if info["validation"](user_input):
break
print("Invalid input. ", end="")
# Do something with `user_input`

输出:

Please enter your first name: 3d3d3d
Invalid input. Please enter your first name: 435345
Invalid input. Please enter your first name: .,.,.,asd3.
Invalid input. Please enter your first name: John.
Invalid input. Please enter your first name: John
Please enter your last name: Doe
Please enter your father's name: Jack
Please enter your age: Twenty
Invalid input. Please enter your age: 20
Please enter your class: what
Invalid input. Please enter your class: 30
>>> 

编辑-这可能更接近你的要求。您有一个人员列表(persons(,它是一个字典列表。您不需要预先定义稍后将填充的字段,只需要定义不会更改的字段即可。person_fields字典将需要用用户输入填充的人员字段或属性的名称映射到具有该字段的输入验证方法和类型转换提示的相应字典。我称之为";漂亮的";只是人类可读/人类友好的或";漂亮的";字段名称的版本(例如,提示打印father's name而不是fathers_name(。

对于人员列表中的每个人,我们遍历所有需要填充的字段。对于每个字段,请求用户输入,直到用户键入有效的内容(如果将user_input作为参数传递,则当前字段的info["validation"]方法返回True,则该字段有效(。一旦他们输入了有效的内容,我们就打破while循环,将该字段添加为当前人员的新键,其中关联的值是转换为当前字段的info["type"]中的类型的验证输入。

persons = [
{
"id": 1001,
"age": 34,
"class": 1
},
{
"id": 1002,
"age": 30,
"class": 3
},
{
"id": 1003,
"age": 16,
"class": 4
}
]
person_fields = {
"name": {
"pretty": "first name",
"validation": str.isalpha,
"type": str
},
"lastname": {
"pretty": "last name",
"validation": str.isalpha,
"type": str
},
"fathers_name": {
"pretty": "father's name",
"validation": str.isalpha,
"type": str
},
"id_number": {
"pretty": "ID number",
"validation": str.isdigit,
"type": int
}
}
for person in persons:
print(f"Person (ID #{person['id']})".center(32, "-"))
for field, info in person_fields.items():
while True:
user_input = input(f"Please enter your {info['pretty']}: ")
if info["validation"](user_input):
break
print("Invalid input. ", end="")
person[field] = info["type"](user_input)

输出:

-------Person (ID #1001)--------
Please enter your first name: John
Please enter your last name: Doe
Please enter your father's name: Jack
Please enter your ID number: one two three
Invalid input. Please enter your ID number: 123
-------Person (ID #1002)--------
Please enter your first name: Bob
Please enter your last name: Bobson
Please enter your father's name: Bill
Please enter your ID number: 000
-------Person (ID #1003)--------
Please enter your first name: Bill
Please enter your last name: Billson
Please enter your father's name: Bob
Please enter your ID number: 001
>>> persons
[{'id': 1001, 'age': 34, 'class': 1, 'name': 'John', 'lastname': 'Doe', 'fathers_name': 'Jack', 'id_number': 123}, {'id': 1002, 'age': 30, 'class': 3, 'name': 'Bob', 'lastname': 'Bobson', 'fathers_name': 'Bill', 'id_number': 0}, {'id': 1003, 'age': 16, 'class': 4, 'name': 'Bill', 'lastname': 'Billson', 'fathers_name': 'Bob', 'id_number': 1}]
>>> 

相关内容

  • 没有找到相关文章

最新更新