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

Che*_*emi 0 python

我刚刚开始学习python这个问题出现在我的脑海中,是否有更短的方法来确定一个字符串是否等于'某事'或'somethingelse'?

例:

input = raw_input("question?")

while input != 'n' and input != 'y':
    #ask question again
Run Code Online (Sandbox Code Playgroud)

mer*_*011 5

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

input = raw_input("question?")

while input not in ['n', 'N']:
    #ask question again
Run Code Online (Sandbox Code Playgroud)

如果您只是尝试接受两种情况,您也可以调用lower输入.

while input.lower() != 'n':
    #ask question again
Run Code Online (Sandbox Code Playgroud)