Skip to content
Advertisement

How can I store a value from a list, after user input select’s which list to use?

I want the user to type in a name, then I want to proceed by using specific values based on that input. My current solution is this:

JavaScript

However, once I start adding more model names it is going to start to look messy, and I’m assuming there is a more efficient way to do this.

Another solution I have played around with is creating a list for each, then attempting to set the variables to an index of a user inputted list.

JavaScript

But when I do this, it is just returning the letter of the input based on index.

Is it possible to return the correct values based on user input?

Advertisement

Answer

You could put your ẁarrior and ravener lists into a dictionary, then you can simply get the corresponding values with a dictionary lookup:

JavaScript

It might make sense to check whether the entered model name actually exists in the lookup dictioary, else you’ll get a KeyError if the user enters a model name that doesn’t exist in the ´ĺookup`:

JavaScript
User contributions licensed under: CC BY-SA
10 People found this is helpful
Advertisement