'How to let the user select an input from a finite list?

Is it possible to ask for selection between multiple choices in Python, without an if loop?

Example:

print "Do you want to enter the door"
raw_input ("Yes or not")

And the user can only choose between the selections.



Solution 1:[1]

If you need to do this on a regular basis, there is a convenient library for this purpose that may help you achieve better user experience easily : inquirer

Disclaimer : As far as i know, it won't work on Windows without some hacks.

You can install inquirer with pip :

pip install inquirer

Example 1 : Multiple choices

One of inquirer's feature is to let users select from a list with the keyboard arrows keys, not requiring them to write their answers. This way you can achieve better UX for your console application.

Here is an example taken from the documentation :

import inquirer
questions = [
  inquirer.List('size',
                message="What size do you need?",
                choices=['Jumbo', 'Large', 'Standard', 'Medium', 'Small', 'Micro'],
            ),
]
answers = inquirer.prompt(questions)
print answers["size"]

Inquirer example

Example 2 : Yes/No questions :

For "Yes/No" questions such as yours, you can even use inquirer's Confirm :

import inquirer
confirm = {
    inquirer.Confirm('confirmed',
                     message="Do you want to enter the door ?" ,
                     default=True),
}
confirmation = inquirer.prompt(confirm)
print confirmation["confirmed"]

Yes no questions with Inquirer

Others useful links :

Inquirer's Github repo

Solution 2:[2]

One possible way to achieve what you appear to require is with a while loop.

print "Do you want to enter the door"
response = None
while response not in {"yes", "no"}:
    response = raw_input("Please enter yes or no: ")
# Now response is either "yes" or "no"

Solution 3:[3]

For an OS agnostic solutions using prompt-toolkit 2 or 3, use questionary

https://github.com/tmbo/questionary

Solution 4:[4]

This is a bit of an overkill for only selecting yes or no but it is a generic solution also working for more then two options. And it is protected for non existing options and will force the user to give a new valid input. This without any imports.

First a function which handles all functionality:

def selectFromDict(options, name):

index = 0
indexValidList = []
print('Select a ' + name + ':')
for optionName in options:
    index = index + 1
    indexValidList.extend([options[optionName]])
    print(str(index) + ') ' + optionName)
inputValid = False
while not inputValid:
    inputRaw = input(name + ': ')
    inputNo = int(inputRaw) - 1
    if inputNo > -1 and inputNo < len(indexValidList):
        selected = indexValidList[inputNo]
        print('Selected ' +  name + ': ' + selected)
        inputValid = True
        break
    else:
        print('Please select a valid ' + name + ' number')

return selected

Then a dict with all options

options = {}
#     [USER OPTION] = PROGRAM RESULT
options['Yes'] = 'yes'
options['No'] = 'no'

And then call the function with the options

# Let user select a month
option = selectFromDict(options, 'option')

The result is:

> Select a option:
> 1) Yes
> 2) No
> option: 3
> Please select a valid option number
> option: 1
> Selected option: yes

As said this is saleable to for instance all months of the year re-using the function above:

months = {}
months['January'] = 'jan'
months['February'] = 'feb'
months['March'] = 'mar'
months['April'] = 'apr'
months['May'] = 'may'
months['June'] = 'jun'
months['July'] = 'jul'
months['August'] = 'aug'
months['September'] = 'sep'
months['October'] = 'oct'
months['November'] = 'nov'
months['December'] = 'dec'

# Let user select a month
month = selectFromDict(months, 'Month')

Example result:

> Select a Month:
> 1) January
> 2) February
> 3) March
> 4) April
> 5) May
> 6) June
> 7) July
> 8) August
> 9) September
> 10) October
> 11) November
> 12) December
> Month: 5
> Selected Month: may

Solution 5:[5]

For those in Python 3, and that want a non case sensitive option:

def ask_user():
    print("Do you want to save?")
    response = ''
    while response not in {"yes", "no"}:
        response = input("Please enter yes or no: ").lower()
    return response == "yes"

And, if I understand Assignment Expressions (PEP 572) correctly, in Python 3.8 you will be able to do this:

def ask_user():
    while r:= input("Do you want to save? (Enter yes/no)").lower() not in {"yes", "no"}:
        pass
    return r == "yes"

Solution 6:[6]

If you use Windows and you need the immediate input with one character, the this should work:

import os

inp = "yn"

if os.system("choice /c:%s /n /m \"Yes or No (Y/N)\"" % inp) - 2:
    # if pressed Y
    print("Yes")
else:
    # if pressed N
    print("No")

P. S.: This code works on Python 3

Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source
Solution 1 Khopa
Solution 2 holdenweb
Solution 3 M.Vanderlee
Solution 4 Wallem89
Solution 5
Solution 6 Somebody