Skip to content
Advertisement

How do I make an list that can be read as either uppercase or lowercase

Im attempting to rewrite an old program of mine, and Im trying to us a list (as an array) to organize the values better than a bunch of variables. I got the array set up, and then set up a if elif else to check input against the values in the array, but then figured I should try and make the input case insensitive, but I then I had to create a similar array that was the same values, but lowercase. That means I have to change the if and elif statements to check for the lowercase array values

Is there an easier way to do this, or am I stuck with the double array?

block = ["Concrete_1","Concrete_2","Concrete_3","Metal_1","Metal_2","Metal_3","Wood_1","Wood_2","Wood_3","Barrier_Block"]
block_lower = ["concrete_1","concrete_2","concrete_3","metal_1","metal_2","metal_3","wood_1","wood_2","wood_3","barrier_block"]
for x in block:
    print(x)
    time.sleep(0.125)
while choice == 0:
    input_str = "Choose a blockn"
    choice = input(input_str)
    if choice.lower() == block_lower[0]:
        print("n",block_[0])
    elif choice.lower() == block_lower[1]:
        print("n",block[1])
    elif choice.lower() == block_lower[2]:
        print("n",block[2])
    elif choice.lower() == block_lower[6]:
        print("n",block[6])
    elif choice.lower() == block_lower[7]:
        print("n",block[7])
    elif choice.lower() == block_lower[8]:
        print("n",block[8])
    elif choice.lower() == block_lower[3]:
        print("n",block[3])
    elif choice.lower() == block_lower[4]:
        print("n",block[4])
    elif choice.lower() == block_lower[5]:
        print("n",block[5])
    elif choice.lower() == block_lower[9]:
        print("n",block[9])

    else:
        print("Idiot") # Error catch to prevent program from crashing due to a mispelled word or someone thinking their smart and trying to break the code
        choice = 0 # Resets the value to 0 so the loop repeats

The else printing Idiot will be changed, its just a filler for now

Advertisement

Answer

You have the right idea. You have to convert the input and expected values to the same case.

I have a few suggestions:

First of all, you don’t need to hardcode the lowercase version of the list. Instead, generate it from the original list:

block_lower = [b.lower() for b in block]

Second, you can use the in operator to check if an item exists in the list:

while choice == 0:
    input_str = "Choose a blockn"
    choice = input(input_str)
    if choice.lower() in block_lower:
        print(f"found {choice.lower()}")
    else:
        print("not found")

In general, if you find yourself indexing an array repeatedly, you should find another way. Often a for loop is called for. In this specific case, we have in to do the looping for us.

Alternatively, you could show the user a numbered list of blocks and have them enter the corresponding number instead of typing out the full block name.

Advertisement