在与Python中的用户输入匹配的列表列表中操作列表?

2024-09-30 22:22:38 发布

您现在位置:Python中文网/ 问答频道 /正文

我正在创建一个虚构的失踪人员数据库。数据库是一个列表,其中包含“标题”:姓氏、名字、出生日期、电话号码、状态、失踪自和在上找到

除了添加新条目外,我希望用户能够通过提供他们希望更新的条目的姓和DoB来更新现有条目。我现在制定如下,但我有困难使这项工作

例如

用户输入:'Pelgrim', '01/01/1900'

所需输出:['Pelgrim', 'Richard', '01/01/1900', '12345643', ...]

correct_entry = False
while correct_entry == False:
    user_query = input(
        "Please provide the last name and DoB (DD/MM/YYYY) of the entry you want to update, separated by a comma: "
    )
    user_query_list = [x.strip() for x in user_query.split(",")]
    user_query_list[0] = str(user_query_list[0]).capitalize()

    for sublist in missing_persons:
        if user_query_list[0] == sublist[0] and user_query_list[1] == sublist[2]:
            print(
                "We have found the following entry to match the last name and DoB you provided: \n"
            )
            print(sublist)
        else:
            print(
                "We could not find an entry to match the information you provided. Please try again."
            )
            

Tags: andtheto用户you数据库条目query