I think you have a typo (missing line
before your range [:]
) but there's another thing as well. If your search_index
has been assigned a string, you can't subtract or add 5 to it.
I'm not sure of the context so you'll have to tweak this to your needs but this addresses those issues:
s=line.split()
if inflected in s:
search_index = s.index(search)
new_line = line[search_index-5:search_index+5]
else:
print("not in this line")