Hugo Teixeira
Hugo Teixeira

Reputation: 65

How to get the input from user?

I wanted to let the user make a choice, like the example below:

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

But I want to do something like, for example, if the user inputs 0 the variable name_db will contain 'All_Groups', instead of 0... How can I do that?

Thanks in advance!

Upvotes: 0

Views: 88

Answers (1)

BIG Strawberry
BIG Strawberry

Reputation: 51

You can make a function that takes the user input and converts it to the actual name you need as such;

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

Upvotes: 1

Related Questions