Python 如何使用两个用户输入简化if-elif代码?

Python 如何使用两个用户输入简化if-elif代码?,python,Python,有没有办法缩短这个时间?我对用户2也有相同的功能,但是我必须用user2\u working\u status再次执行所有操作,代码太长,因为我有9个选项。因此,如果有一种方法可以在代码中同时使用user1和user2?:)您可以创建一个字典,其中包含每个状态值的标签,如下所示: with open("bankaccount.txt", 'a+') as f: if User1_working_status =="1": f.write("{}\n{}\n{}\n{}\n

有没有办法缩短这个时间?我对用户2也有相同的功能,但是我必须用user2\u working\u status再次执行所有操作,代码太长,因为我有9个选项。因此,如果有一种方法可以在代码中同时使用user1和user2?:)

您可以创建一个字典,其中包含每个状态值的标签,如下所示:

with open("bankaccount.txt", 'a+') as f:
    if User1_working_status =="1":
        f.write("{}\n{}\n{}\n{}\n{}\n{}\n{}\n{}\n{}\n".format("Name: "     + user1_title + " " + user1_name,        
        "Gender:" + user1_gender,"Town and country: "
        + user1_town, "Age: " + user1_age,"Country and town of birth: "+ user1_country_of_birth,"Nationality: "
        + user1_nationality,"Country residence:"+user1_country_residence,"Tax resident country: "
        + user1_tax_res,"Working status: Employed"))
        print("Working status: Employed")


    elif User1_working_status=="2":
        f.write("{}\n{}\n{}\n{}\n{}\n{}\n{}\n{}\n{}\n".format("Name: " + user1_title + " " + user1_name, "Gender:"
        + user1_gender,"Town and country: " + user1_town, "Age: " + user1_age,"Country and town of birth: "
        + user1_country_of_birth,"Nationality: "+ user1_nationality,
        "Country residence:"+user1_country_residence,"Tax resident country: "+ user1_tax_res,
        "Working status: Self Employed"))
        print("Working status: Self Employed")
status_labels = {
    "1": "Employed",
    "2": "Self Employed"
}

with open("bankaccount.txt", 'a+') as f:
    ...
    print("Working status: {}".format(status_labels[User1_working_status]))
编辑:对于多个用户,最好根据迭代一个dict列表并使用str.format(**dict),例如:

with open("bankaccount.txt", 'a+') as f:
    if User1_working_status =="1":
        f.write("{}\n{}\n{}\n{}\n{}\n{}\n{}\n{}\n{}\n".format("Name: "     + user1_title + " " + user1_name,        
        "Gender:" + user1_gender,"Town and country: "
        + user1_town, "Age: " + user1_age,"Country and town of birth: "+ user1_country_of_birth,"Nationality: "
        + user1_nationality,"Country residence:"+user1_country_residence,"Tax resident country: "
        + user1_tax_res,"Working status: Employed"))
        print("Working status: Employed")


    elif User1_working_status=="2":
        f.write("{}\n{}\n{}\n{}\n{}\n{}\n{}\n{}\n{}\n".format("Name: " + user1_title + " " + user1_name, "Gender:"
        + user1_gender,"Town and country: " + user1_town, "Age: " + user1_age,"Country and town of birth: "
        + user1_country_of_birth,"Nationality: "+ user1_nationality,
        "Country residence:"+user1_country_residence,"Tax resident country: "+ user1_tax_res,
        "Working status: Self Employed"))
        print("Working status: Self Employed")
status_labels = {
    "1": "Employed",
    "2": "Self Employed"
}

with open("bankaccount.txt", 'a+') as f:
    ...
    print("Working status: {}".format(status_labels[User1_working_status]))

长格式字符串也很难读取。这个怎么样

users = [{"title": "dd", "working_status": ...}, ...]

with open("bankaccount.txt", 'a+') as f:
    for user in users:
        ..."Name: {title}, Working status: {working_status}".format(**user)

有。你能列出这两种情况之间的所有差异吗?对于用户2来说是一样的。我只需将User1\u working\u状态切换为User2\u working\u状态,将User1\u title切换为User2\u title等等:)如果您逐行编写,并对一行使用
.format()
,它的可读性会更高。我打算提出同样的建议。我还建议您阅读,这将极大地提高可读性和简化您的代码,并更新关于多个用户的建议,希望如此helps@buran如果要建议使用
status\u标签,dict中缺少键会导致故意异常。get(User1\u working\u status,“Unknown”)
,请不要修改我的答案,谢谢!