I am writing a Python program to do page statistics calculations. I need to grep the word sequence by sequence. If the word 'PRECHARGE' appears before 'ACTIVE', i.e. from Line 1 to 2, the value of A will be equal to one as shown in the script below. If the word 'PRECHARGE' appears after 'ACTIVE', i.e. from Line 3 to 4, the value of error will be equal to 1.
So in this example, I should have A = 1 and Error = 1. When I print the value of A and Error, respectively, they are showing A = 2, which is untrue. How to resolve this logical error?
import re
A = 0
error = 0
lines = open("page_stats_ver2.txt", "r").readlines()
for line in lines:
if re.search(r"ACTIVE", line):
if re.search(r"PRECHARGE", line):
error += 1
else:
A += 1
print(A)
print(error)