lines = open("input.txt", 'r').read().splitlines() sum = 0 words = ["zero", "one", "two", "three", "four", "five", "six", "seven", "eight", "nine"] for l in lines: first = "" firstFound = False last = "" lastFound = False n = len(l) for i in range(n): if not firstFound and l[i].isnumeric(): first = l[i] firstFound = True if not lastFound and l[-i-1].isnumeric(): last = l[-i-1] lastFound = True for j in range(len(words)): m = len(words[j]) if not firstFound and l[i:i+m] == words[j]: first = str(j) firstFound = True if not lastFound and l[-(i+m):n-i] == words[j]: last = str(j) lastFound = True if firstFound and lastFound: break s = first + last sum += int(s) print(sum)