Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #инициализация переменных
- num_sum = 0.0
- lucky_num = 0
- all_tickets_num = 0
- #читаем данные из файла
- with open("input.txt") as input:
- for line in input:
- try :
- #Преобразуем строку в массив чисел и обрабатываем
- numbers = [int(i) for i in line.strip()]
- all_tickets_num += 1
- if len(numbers) != 6:
- print('Incorrect data format in this line:',
- line)
- break
- left_sum = sum(numbers[:3])
- right_sum = sum(numbers[3:])
- lucky_num += int(left_sum == right_sum)
- num_sum += (left_sum + right_sum)
- except Exception as e:
- #Если возникла ошибка при обработке строки, выводим строку и лог ошибки
- print('Incorrect data format in this line:',
- line)
- print('Error:',e)
- break
- #Проверка на пустой файл
- if all_tickets_num == 0:
- print('File is empty')
- else:
- #Вывод в файл
- out = open('output.txt', 'w')
- out.write( '{}\n{:.3}'.format( lucky_num, num_sum/(6*all_tickets_num) ))
- out.close()
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement