close

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

Hello Guys, How are you all? Hope You all Are Fine. Today We Are Going To learn about How to let the user select an input from a finite list in Python. So Here I am Explain to you all the possible Methods here.

Without wasting your time, Let’s start This Article.

Table of Contents

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

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

    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.

  2. let the user select an input from a finite list

    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.

Method 1

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"

Method 2

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

Summery

It’s all About this issue. Hope all Methods helped you a lot. Comment below Your thoughts and your queries. Also, Comment below which Method worked for you? Thank You.

Also, Read