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?

JavaScript

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:

JavaScript

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

JavaScript

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