python 如何获得用户的输入?

pgx2nnw8  于 2023-01-04  发布在  Python
关注(0)|答案(1)|浏览(106)

我想让用户做出选择,如下面的例子所示:

group = input("Which group you want to analyze: \n 0-All_Groups \n 1-Caltech \n 2-KKI \n 3-Leuven)
name_db = group

但是我想做一些事情,例如,如果用户输入0,变量name_db将包含'All_Groups',而不是0...我该怎么做呢?
先谢了!

wydwbb8l

wydwbb8l1#

您可以创建一个函数,接受用户输入并将其转换为您需要的实际名称;

def get_group_name_from_id(id):
    if id == "0":
        return "All_Groups"
    elif id == "1":
        return "Caltech"
    elif id == "2":
        return "KKI"
    elif id == "3":
        return "Leuven"
    else:
        return "an invalid option"

group_id = input("Which group you want to analyze: \n 0-All_Groups \n 1-Caltech \n 2-KKI \n 3-Leuven")
group_name = get_group_name_from_id(group_id)
print("You have chosen:", group_name, "!")

相关问题