如何获取用户的输入?

How to get the input from user?

我想让用户做出选择,就像下面的例子:

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... 我该怎么做?

提前致谢!

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

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, "!")