Python Forum
Variable for the value element in the index function??
Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
Variable for the value element in the index function??
#1
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!
Reply


Messages In This Thread
Variable for the value element in the index function?? - by Learner1 - Jan-20-2024, 12:16 AM

Possibly Related Threads…
Thread Author Replies Views Last Post
  Variable is not defined error when trying to use my custom function code fnafgamer239 4 715 Nov-23-2023, 02:53 PM
Last Post: rob101
  Printing the variable from defined function jws 7 1,559 Sep-03-2023, 03:22 PM
Last Post: deanhystad
  Function parameter not writing to variable Karp 5 1,084 Aug-07-2023, 05:58 PM
Last Post: Karp
  Index Function not recognized in Python 3 Peter_B_23 1 1,427 Jan-08-2023, 04:52 AM
Last Post: deanhystad
  Retrieve variable from function labgoggles 2 1,118 Jul-01-2022, 07:23 PM
Last Post: labgoggles
  Cant transfer a variable onto another function KEIKAS 5 1,996 Feb-09-2022, 10:17 PM
Last Post: deanhystad
  myList.insert(index, element) question ChrisF 1 1,707 Aug-27-2021, 03:49 PM
Last Post: bowlofred
  How i can add elements to table index of element blazej2533 3 2,090 Dec-03-2020, 08:16 PM
Last Post: Larz60+
Question Matching variable to a list index Gilush 17 6,131 Nov-30-2020, 01:06 AM
Last Post: Larz60+
  How to get index of minimum element between 3 & 8 in list Mekala 2 2,616 Nov-10-2020, 12:56 PM
Last Post: DeaD_EyE

Forum Jump:

User Panel Messages

Announcements
Announcement #1 8/1/2020
Announcement #2 8/2/2020
Announcement #3 8/6/2020