USACO 2019 February Contest, Bronze Problem 3. Measuring Traffic

I’ve been working on this problem for a while, but my code can only pass the first two test cases. I downloaded the test data for the problem, but anything I change will make my code give the wrong answer for the first two test cases.
here is my (python) code:
“”"
ID: yayaliu1
LANG: PYTHON3
TASK: billboard
“”"

fin = open(r"traffic.in", ‘r’)
fout = open(r"traffic.out",‘w’)

def find(M):
for i in M:
if i[0] == 0:
return int(M.index(i))

def after(ramp, tracked, first):
return [first[1]+ramptracked[1],first[2]+ramptracked[2]]

def ramp(ramp):
if ramp == ‘on’:
return 1
elif ramp == ‘off’:
return -1
else:
return 0

def before(ramp, tracked, first):
return [first[1]-ramptracked[1],first[2]-ramptracked[2]]

N = int(fin.readline())
M = []
for i in range(int(N)):
M.append(fin.readline().split())
for i in M:
i[0] = ramp(i[0])

M = [[int(i) for i in q] for q in M]
print(M)

q = -1
low = -float(‘inf’)
high = float(‘inf’)
M.reverse()
for i in M:
if i[0]==0:
low = max(low,i[1])
high = min(high,i[2])
else:
high+=q*i[0]i[1]
low+=q
i[0]*i[2]
low = max(0,low)
print(i,low,high)
fout.write(str(low)+’ ‘+str(high)+’\n’)
M.reverse()
q = 1
low = -float(‘inf’)
high = float(‘inf’)
for i in M:
if i[0]==0:
low = max(low,i[1])
high = min(high,i[2])
else:
high+=i[0]*i[1]
low+=i[0]*i[2]
low = max(0,low)
print(i,low,high)
fout.write(str(low)+’ ‘+str(high)+’\n’)

fin.close()
fout.close()