0

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!

1 Answer 1

1

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, "!") 
Sign up to request clarification or add additional context in comments.

2 Comments

@HugoTeixeira the input() function simply prints the string passed to it and returns the user input when it is entered. Input() has no clue what the string contains or what options you are presenting the user with. So as far as I am aware you'll need to use some logic to convert the user input to name.
thanks for your explanation and availability!

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.