tenebris silentio
tenebris silentio

Reputation: 519

Python - Select string from a list based on index number - index number entered via input

I'm trying to help someone extract a list of facilities from a list without having to enter the full name of the facility. Instead, I thought it would be easier to associate a number with each facility and allow the user to just enter the corresponding number to get the string. For example, if I have a list of 'facility a', 'facility b', and 'facility c', if the user entered '0', 'facility a' would show up, 1 'facility b' would show up, etc. I know my code is incorrect, but I am struggling to make this work based on the number a user enters through the input command. See code below.

facilities = ['facility a', 'facility b', 'facility c']

for count, value in enumerate(facilities):
    j = (count, value)
    print(j) #This is just so the user can see all possible options in the list 

f = input('Please enter the corresponding number of the facility you want to select: ')

final_facility = facilities[f] # I know this doesn't work and it wants a number, not a string

print(final_facility)

Upvotes: 0

Views: 1266

Answers (6)

Rohit Patil
Rohit Patil

Reputation: 171

Cast the input to a integer using int, like so:

f = int(input('Please enter the corresponding number of the facility you want to select: '))

To be precise, whenever you want to access the element at a index do, int(f) instead of directly passing f.

Upvotes: 2

Sagar Mehta
Sagar Mehta

Reputation: 453

facilities = ['facility a', 'facility b', 'facility c']

for count, value in enumerate(facilities):
    j = (count, value)
    print(j) #This is just so the user can see all possible options in the list

f = input('Please enter the corresponding number of the facility you want to select: ')

final_facility = facilities[int(f)] # Pass int(f) instead of f

print(final_facility)

or

facilities = ['facility a', 'facility b', 'facility c']

for count, value in enumerate(facilities):
    j = (count, value)
    print(j) #This is just so the user can see all possible options in the list

f = int(input('Please enter the corresponding number of the facility you want to select: ')) # Typecast your input to int

final_facility = facilities[f] 

print(final_facility)

make sure to handle exceptions for ValueErrors.

Upvotes: 2

Sefan
Sefan

Reputation: 709

You need to check if the input is numeric and then convert it to an int. Like this:

facilities = ['facility a', 'facility b', 'facility c']

for count, value in enumerate(facilities):
    j = (count, value)
    print(j) #This is just so the user can see all possible options in the list 

f = input('Please enter the corresponding number of the facility you want to select: ')

final_facility = 'Please eneter a number' # Prints this if f is not numeric
if f.isnumeric():
    final_facility = facilities[int(f)] # I know this doesn't work and it wants a number, not a string

print(final_facility)

Upvotes: 2

mutedspeaker
mutedspeaker

Reputation: 32

Just before final facility, write f=int(f) That should solve it. Because, the user input, is by default a string in python, so now, we have typecasted the f to an integer which solves it. Also try using dictionary which will remove this complete hassle altogether. Also to get the answer, after the for loop you can directly just write: print(facilities (int(f))).

Upvotes: 1

Cezar Prodan
Cezar Prodan

Reputation: 100

Alternatively you can automatically typecast your input as: f = int(input('Please ...'))

Upvotes: 2

Python learner
Python learner

Reputation: 1145

By default, input() returns a string. But, list indices can't be strings. So, it needs to be like this

f = int(input('Please enter the corresponding number of the facility you want to select: '))

Upvotes: 2

Related Questions