0

Total noob question, but I'm trying to modify someone else's code.

The current line is:

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

And I need it to be something like:

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

Basically checking for two different words.
This is currently in Python 2.7

Things I've tried that have generated different errors:

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]:

Any help is appreciated!

Full Loop Code:

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

2 Answers 2

1

I think this should work:

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

This should work if you want to run the while loop while neither of these words is in text. If you want to keep running the loop if just one of the words is not in text, you can replace the and with or.

Sign up to request clarification or add additional context in comments.

1 Comment

@Reese: then show us your loop code. The index out of range error has nothing to do with the logic problem you presented us. It just means i is not a valid index for text.
1

You need and:

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

because both conditions must be met, not either. Following De Morgan's laws you could also express that as:

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

e.g. if either 'Normal' or 'Error' are found in text[i], then the while loop should end.

Since this is using strings, you can also use a regular expression:

import re

while not re.match(r'(?:Normal|Error)', text[i]):

where the regular expression matches if either 'Normal' or 'Error' are found in text[i].

In your loop, you never test if i is smaller than the total number of items; you need to add a test for that too:

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

5 Comments

Pretty sure I want or not and. If it finds either words then it needs to move on to the next stage.
Nice use of isdisjoint.
@Reese: the condition keeps the loop going. If either word is found then one of the two statements is False and the loop ends, moving to the next stage.
I'm pretty sure isdisjoint is wrong, since it looks like text[i] is just a string, not a set. remember that string1 in string2 is completely different than object in container, since it searches for a sequence, not a single element.
@o11c: take into account that the OP edited the question after I posted this; there was no indication that text[i] was a string. I used it on the basis that text[i] was a list of strings instead.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.