gpt4 book ai didi

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

转载 作者:行者123 更新时间:2023-12-01 21:23:08 24 4
gpt4 key购买 nike

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

即请只给出数字。请仅提供字符串。

我可以验证用户“姓名”,但如何对第二个变量(“姓氏”)进行验证。

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,例如)。

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

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}]
>>>

关于Python 3 - 如何验证多个用户输入,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/63547803/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com