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

Time:10-27

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)

CodePudding user response:

cast the input to a integer using int.

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

CodePudding user response:

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: '))

CodePudding user response:

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

CodePudding user response:

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))).

CodePudding user response:

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)

CodePudding user response:

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.

  • Related