[英]How to compare a user input against multiple OR conditions PYTHON 3.x
Apologies if this is a dumb question.如果这是一个愚蠢的问题,请道歉。 I've done a bit of searching, and haven't been able to find the info I need though.我已经做了一些搜索,但一直无法找到我需要的信息。 I'm very new to python.我对python很陌生。 Currently in the middle of the Learn Python 3 The Hard Way course.目前在 Learn Python 3 The Hard Way 课程的中间。
I'm trying to write an IF statement that takes a user generated string, and compares it against a list, and then evaluates to True, if there is a match.我正在尝试编写一个 IF 语句,该语句采用用户生成的字符串,并将其与列表进行比较,如果匹配,则评估为 True。
I have been able to do this successfully using:我已经能够成功地做到这一点:
if input in list:
print("That was in the list.")
but what I'm trying to do now is swap this around and use a one off list that's part of the IF statement.但是我现在要做的是交换它并使用作为 IF 语句一部分的一次性列表。 I'm doing a ZORK style game where there are rooms with doors in different walls etc, so in this case it didn't make sense to me to have a bunch of permanent lists with different configurations of 'n', 's', 'e', 'w' in them that I'd have to reference depending on which walls have doors.我正在做一个 ZORK 风格的游戏,其中房间的门在不同的墙壁等处,所以在这种情况下,拥有一堆具有不同配置的“n”、“s”的永久列表对我来说没有意义, 'e'、'w' 在其中我必须根据哪些墙有门来引用。 But I don't want to write out three separate elif evaluations that all do the exact same thing either (if I wrote one for each 'no go' direction in each room.) Hope that all makes sense.但我不想写出三个独立的 elif 评估,它们都做完全相同的事情(如果我为每个房间的每个“禁止”方向写一个)。希望一切都有意义。
I read somewhere that you could put a list into an IF statement like this {'up', 'down', 'left'} But when I try that it says that I don't have a string in my "in" evaluation:我在某处读到,您可以将列表放入 IF 语句中,例如 {'up', 'down', 'left'} 但是当我尝试这样做时,它说我的“in”评估中没有字符串:
choice = input("> ")
if {'up', 'down', 'left', 'right'} in choice:
print("You wrote a direction!")
else:
print("Oh bummer.")
All you need to do is use a list []
square brackets, instead of curly ones (those are for sets) and you need to move the choice variable ahead.您需要做的就是使用列表[]
方括号,而不是花括号(那些用于集合),并且您需要将选择变量向前移动。 ( You want to see that the choice
is in the list, and not the other way around. ) (您希望看到choice
在列表中,而不是相反。)
Your code should be :你的代码应该是:
choice = input("> ")
if choice in ['up', 'down', 'left', 'right']:
print("You wrote a direction!")
else:
print("Oh bummer.")
Wrong order错误的顺序
if choice in {'up', 'down', 'left', 'right'}:
print("You wrote a direction!")
else:
print("Oh bummer.")
EDIT: Use sets for existence-checking is usually more efficient than lists编辑:使用集合进行存在检查通常比列表更有效
You can use any()
to check if any of 'up'
, 'down'
, 'left'
, 'right'
string exists in your choice
variable:您可以使用any()
检查您的choice
变量中是否存在'up'
、 'down'
、 'left'
、 'right'
字符串中的任何一个:
choice = input("> ")
if any(x in choice for x in {'up', 'down', 'left', 'right'}):
print("You wrote a direction!")
else:
print("Oh bummer.")
The input format is usually pre-determined by the program though, so you could probably doing something like this:输入格式通常是由程序预先确定的,所以你可能会做这样的事情:
choice = input("> ")
# assuming the input is always in the format of "go <direction>"
direction = choice.split()[1]
if direction in {'up', 'down', 'left', 'right'}:
print("You wrote a direction!")
else:
print("Oh bummer.")
Or maybe you could use regex (but that's rather more complicated)或者也许你可以使用正则表达式(但这更复杂)
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.