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.
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.
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"]
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"]
Others useful links :
PyInquirer
which works great. And here is the link to their documentation: github.com/CITGuru/PyInquirer –
Frilling 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
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"
{"yes", "no"}
better / faster than using a list? –
Lights __contains__
method, which for a list iterates over each element until it finds a match or exhausts all the elements. For a set, hashing techniques are used to give almost constant execution time regardless of the number of elements. –
Woodcock For an OS agnostic solutions using prompt-toolkit 2 or 3, use questionary
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"
pass
loop body will, I suspect, increase cognitive load for most readers. A while
loop with a real body and the right initial condition seemed the more natural formulation to me. There's always room for differences of opinion in programming. –
Woodcock response = input(...).lower()
. Then you can also do response = None
for a more solid sentinel value. –
Bilection response=None
making it a more solid sentinel value, feel free to edit directly. –
Appurtenance 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
© 2022 - 2024 — McMap. All rights reserved.
if
is not a loop. Why don't you want to useif
? – Overleap