#Import the module that give us access to work with files. Allows us to manage the file-related input. from io import open # We open the txt document containing the calibration code calibration_document=open('calibration_document.txt','r') # We read the text inside distinguishing by lines text=calibration_document.readlines() # Close the txt file to be able to work on the text without having the file open. calibration_document.close() del(calibration_document) # Define the digits that we want to search in the text numbers = ['0','1','2','3','4','5','6','7','8','9'] calibration_value=0 list_numbers=[] # Combining the first digit and the last digit to form a single two-digit number to obtein the calibration values for line in text: code='' for letter in line: if letter in numbers: code=code+letter break for letter in line[::-1]: if letter in numbers: code=code+letter break list_numbers.append(int(code)) # The sum of all of the calibration values is the solution of the problem to to repare the trebuchet calibration_values=sum(list_numbers) #print(list_numbers) print('The calibration value is {}'.format(calibration_values))