Jan-20-2024, 12:16 AM
Let's say that I have a list with all the letters of the English alphabet in it.
Is it possible to allow a user to input an alpha character,
then assign that character to a variable,
and then use that variable in the index function (or some other way) to find the index number of the corresponding letter in the list?
My failed attempt looked something like this:
alpha_char = input("Enter a letter of the English alphabet: ")
index_num = alphabet_list.index(alpha_char)
The error that I received was: ValueError: ' ' is not in the list
I know that the input process worked, because I used a print statement right before the error line, and I got the user input correctly printed. Somehow, trying to use a variable as the index element seems to have converted it to a space or a null character (I think). I searched the web for an answer, for a while, but everyone wanted to explain how to go the opposite direction (getting the index number as input from the user and then fiding the value that corresponds to it in the list).
Thanks for any help!
Is it possible to allow a user to input an alpha character,
then assign that character to a variable,
and then use that variable in the index function (or some other way) to find the index number of the corresponding letter in the list?
My failed attempt looked something like this:
alpha_char = input("Enter a letter of the English alphabet: ")
index_num = alphabet_list.index(alpha_char)
The error that I received was: ValueError: ' ' is not in the list
I know that the input process worked, because I used a print statement right before the error line, and I got the user input correctly printed. Somehow, trying to use a variable as the index element seems to have converted it to a space or a null character (I think). I searched the web for an answer, for a while, but everyone wanted to explain how to go the opposite direction (getting the index number as input from the user and then fiding the value that corresponds to it in the list).
Thanks for any help!