How to get the input from user?

Question:

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!

Asked By: Hugo Teixeira

||

Answers:

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, "!")
Answered By: Wouter Dijkstra
Categories: questions Tags: ,
Answers are sorted by their score. The answer accepted by the question owner as the best is marked with
at the top-right corner.