Python中While循环中的多个条件

Python中While循环中的多个条件,python,python-2.7,while-loop,Python,Python 2.7,While Loop,完全没有问题,但我正试图修改其他人的代码 当前行为: while 'Normal Stage' not in text[i]: 我需要它是这样的: while ('Normal Stage', 'Stage Error') not in text[i]: 基本上检查两个不同的单词。 这是Python2.7中的当前版本 我尝试过的产生不同错误的方法: while 'Normal Stage' not in text[i] or 'Error Stage' not in text[i]: wh

完全没有问题,但我正试图修改其他人的代码

当前行为:

while 'Normal Stage' not in text[i]:
我需要它是这样的:

while ('Normal Stage', 'Stage Error') not in text[i]:
基本上检查两个不同的单词。
这是Python2.7中的当前版本

我尝试过的产生不同错误的方法:

while 'Normal Stage' not in text[i] or 'Error Stage' not in text[i]:

while text[i] not in ('Normal Stage', 'Error Stage'): 

while ('Normal Stage', 'Error Stage') not in text[i]:
感谢您的帮助

全循环代码:

i = 0
f = False
while ('Normal Error', 'Stage Error').isdisjoint(text[i]):
    if 'Findings' in text[i]:
        d['Findings'] = (text[i].split(':'))[1].strip()
        f = True
    elif f == True:
        d['Findings'] += "\n" + text[i].strip()
    i += 1

您需要

while 'Normal' not in text[i] and 'Error' not in text[i]:
因为这两个条件都必须满足,两者都不能满足。以下内容也可以表示为:

while not ('Normal' in text[i] or 'Error' in text[i]):
e、 g.如果在
文本[i]
中发现
'Normal'
'Error'
,则
while
循环应结束

由于这是使用字符串,因此还可以使用:

如果在
文本[i]
中找到
'Normal'
'Error'
,则正则表达式匹配

在循环中,您从不测试
i
是否小于项目总数;您还需要为此添加一个测试:

while i < len(text) and not ('Normal' in text[i] or 'Error' in text[i]):
当i
我认为这应该有效:

while ('Normal' not in text[i]) and ('Error' not in text[i]):

如果您希望在这些单词都不在文本中时运行while循环,那么这应该可以工作。如果只想在其中一个单词不在文本中时继续运行循环,可以将and替换为or。

非常确定我想要还是不想要and。如果它找到了任何一个单词,那么它就需要进入下一个阶段。如果找到任何一个单词,那么两个语句中的一个是
False
,循环结束,进入下一个阶段。我很确定
isdisjoint
是错误的,因为它看起来像
text[I]
只是一个字符串,而不是一个集合。请记住,string2中的
string1与容器中的
对象完全不同,因为它搜索的是一个序列,而不是一个元素。@o11c:请考虑到OP在我发布该问题后编辑了该问题;没有迹象表明
text[i]
是字符串。我之所以使用它,是因为
text[I]
是一个字符串列表。@Reese:然后给我们看看你的循环代码。索引超出范围错误与您向我们提出的逻辑问题无关。它只是意味着
i
不是
文本的有效索引。
while ('Normal' not in text[i]) and ('Error' not in text[i]):