-1

How do I check user input against multiple lists in python?

Ex. I want to check if an input is in one of four lists. One list for up down left and right. Each list has different acceptable ways to make the program continue. Once the input is verified to be in one of the lists I will need to figure out how to make it check against the individual lists so that the input correlates correctly to the desired direction.

Custom characters are used in two spots but they print properly.

Current Code:

left = "<-"
right = "->"
up = "↑"
down = "↓"

up_list = ["up", "Up", "UP"];
down_list = ["down", "Down", "DOWN"];
right_list = ["right", "Right", "RIGHT"];
left_list = ["left", "Left", "LEFT"];

print("What would you like to do?")
print("Movement Choices: Up, Down, Left, Right: ")
option = input("Choice: ")

if option in up_list:
    print(up)
if option in down_list:
    print(down)
if option in left_list:
    print(left)
if option in right_list:
    print(right)

if option not in up_list, down_list, right_list, left_list:
    print("error")
Marcus Vinicius Pompeu
  • 1,219
  • 1
  • 11
  • 24
  • Please, state what's wrong if the current code. I have a hunch, but we contributors may not contribute on hunches :-) – Marcus Vinicius Pompeu Jun 16 '21 at 00:27
  • Right now its that im using commas to try to make it check each list, i have no idea how to so i did that to just try something and see what happened. – Dawson Miller Jun 16 '21 at 00:30
  • those are not commas but semi-collens and they are useless in this case (the ones next to lists) – Matiiss Jun 16 '21 at 00:32
  • i was looking at the if option not in section, the rest currently runs fine but the option not in line gives an error because of the commas – Dawson Miller Jun 16 '21 at 00:35
  • though i do see what you mean by the semi colons by the lists are useless – Dawson Miller Jun 16 '21 at 00:37
  • @DawsonMiller, that was my hunch. You had a simple syntax error. And, again, you did never state the real problem. I asked what was wrong with your code and the expected answer was 'there is a syntax error in the line XX'. Please, in the next questions, try to be more specific. I'm down-voting you question, sorry. – Marcus Vinicius Pompeu Jun 16 '21 at 00:54
  • @MarcusViniciusPompeu interestingly my hunch was that OP wanted to make the evaluation process more compact not that there were any errors (which I noticed only now) – Matiiss Jun 16 '21 at 00:57
  • 1
    Oh, ok yeah that is definitely on me, I'll make sure to do that, sorry. – Dawson Miller Jun 16 '21 at 00:57
  • @DawsonMiller, kudos! :-) For every mistake, a bunch of knowledge! I'll undo the downvote. Let's go, to the infinity and beyond! :-) – Marcus Vinicius Pompeu Jun 16 '21 at 01:03

3 Answers3

0

If this suit your need?please make it clear what you want

directions = {
    "<-":["left", "Left", "LEFT"],
    "->":["right", "Right", "RIGHT"],
    "↑":["up", "Up", "UP"],
    "↓":["down", "Down", "DOWN"],
}

print("What would you like to do?")
print("Movement Choices: Up, Down, Left, Right: ")
option = input("Choice: ")

match = False
for key,value in directions.items():
    if option in value:
        print(key)
        match = True
        break

if match == False:
    print("error")
nay
  • 1,725
  • 1
  • 11
  • 11
  • btw usually the part that is going to get evaluated is the key and value is what is associated with those keys so usually those key value pairs would be reversed (btw lists can't be keys, they would have to be converted to tuples) – Matiiss Jun 16 '21 at 00:48
0

@DawsonMiller, you have a syntax error in

if option not in up_list, down_list, right_list, left_list:

which could be rewritten as

if option not in up_list + down_list + right_list + left_list:

The complete solution:

left = "<-"
right = "->"
up = "↑"
down = "↓"

up_list = ["up", "Up", "UP"];
down_list = ["down", "Down", "DOWN"];
right_list = ["right", "Right", "RIGHT"];
left_list = ["left", "Left", "LEFT"];

print("What would you like to do?")
print("Movement Choices: Up, Down, Left, Right: ")
option = input("Choice: ")

if option in up_list:
    print(up)
if option in down_list:
    print(down)
if option in left_list:
    print(left)
if option in right_list:
    print(right)

if option not in up_list + down_list + right_list + left_list:
    print("error")
Marcus Vinicius Pompeu
  • 1,219
  • 1
  • 11
  • 24
-1

So here is what You probably want:

left = "<-"
right = "->"
up = "↑"
down = "↓"

up_list = ["up", "Up", "UP"]
down_list = ["down", "Down", "DOWN"]
right_list = ["right", "Right", "RIGHT"]
left_list = ["left", "Left", "LEFT"]


dct = {tuple(up_list): up, tuple(down_list): down, tuple(right_list): right, tuple(left_list): left}

print("What would you like to do?")
print("Movement Choices: Up, Down, Left, Right: ")
option = input("Choice: ")

for key, value in dct.items():
    if option in key:
        print(value)
        break

it is using dictionaries however there is another way which makes it a bit easier and better:

left = "<-"
right = "->"
up = "↑"
down = "↓"

dct = {'up': up, 'down': down, 'right': right, 'left': left}

print("What would you like to do?")
print("Movement Choices: Up, Down, Left, Right: ")
option = input("Choice: ")

print(dct[option.lower()])

as You can notice there is only one key there and also the evaluation process is way shorter, just one line because this use string method .lower() which means that the inputed string will be transformed to all lower so it will match the key case so just print out the value (for this something like try/except should be used for cases when the key is not in dictionary)

Matiiss
  • 5,970
  • 2
  • 12
  • 29
  • so, why was this downvoted? I could guess the OP didn't clarify enough, but... – Matiiss Jun 16 '21 at 00:41
  • That does do it, so how does the dct work? – Dawson Miller Jun 16 '21 at 00:41
  • `dct` is just a variable name. it is just a simple python dictionary, there is plenty information about them but they are pretty basic, here are some sources: [basics I guess](https://www.w3schools.com/python/python_dictionaries.asp); [the part about tuples as keys](https://stackoverflow.com/a/2974082/14531062) – Matiiss Jun 16 '21 at 00:44