Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/python/325.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Python:检查一个字符串是否等于其他两个字符串的较短方法_Python - Fatal编程技术网

Python:检查一个字符串是否等于其他两个字符串的较短方法

Python:检查一个字符串是否等于其他两个字符串的较短方法,python,Python,我刚开始学习python,我就想到了这个问题 有没有一种更短的方法来确定字符串是否等于'something'或'somethingelse' 例如: input = raw_input("question?") while input != 'n' and input != 'y': #ask question again 您可以检查它是否在列表或集合中 input = raw_input("question?") while input not in ['n', 'N']:

我刚开始学习python,我就想到了这个问题 有没有一种更短的方法来确定字符串是否等于'something'或'somethingelse'

例如:

input = raw_input("question?")

while input != 'n' and input != 'y':
    #ask question again

您可以检查它是否在列表或集合中

input = raw_input("question?")

while input not in ['n', 'N']:
    #ask question again
但是,如果您只是尝试接受两种情况,您也可以对输入调用
lower

while input.lower() != 'n':
    #ask question again

如果是案例问题,那么您可以:

while input.lower() != 'n'

也许出乎意料,
'a'!=输入!='b'
有效。它的解析方式与
('a'!=input)和('b'!=input)
相同。您也可以对数字使用
==
等进行相同的操作


哦,但是如果你将它链接到三个以上的东西,或者使用多个不同的比较运算符,你必须小心。

或者一组
{'n','n'}
当我尝试使用'is not'时,它不起作用,为什么呢?在检查字符串值时,永远不要使用
is
。它检查内存中的引用@chemi@Chemi,如果这个答案能解决您的问题,请接受。这对您很有帮助,谢谢!