holaprofesor
holaprofesor

Reputation: 271

selection from a list python

I want to be able to accept user inputs from a list of strings as arguments, such as

x = get_user_input(['yes', 'no', 'maybe'])

If the user doesn't enter an input from the list. Finally, the program should return the part of the list that the user selects.

Upvotes: 0

Views: 62

Answers (1)

Ananth
Ananth

Reputation: 4407

def get_user_input(allowed_choices):
    while True:
        s = raw_input("Enter your choice " + "/".join(allowed_choices) + ": ")
        if s in allowed_choices:
            return s

x = get_user_input(['yes', 'no', 'maybe'])

If you want case insensitive comparison, use s.lower() in allowed_choices.

Upvotes: 3

Related Questions