Advertisement
Guest User

Untitled

a guest
Jan 21st, 2020
104
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Python 1.34 KB | None | 0 0
  1. #инициализация переменных
  2. num_sum = 0.0
  3. lucky_num = 0
  4. all_tickets_num = 0
  5.  
  6. #читаем данные из файла
  7. with open("input.txt") as input:
  8.     for line in input:
  9.         try :
  10.             #Преобразуем строку в массив чисел и обрабатываем
  11.             numbers = [int(i) for i in line.strip()]
  12.             all_tickets_num += 1
  13.             if len(numbers) != 6:
  14.                 print('Incorrect data format in this line:',
  15.                  line)
  16.                 break
  17.            
  18.             left_sum =  sum(numbers[:3])
  19.             right_sum = sum(numbers[3:])
  20.             lucky_num += int(left_sum == right_sum)
  21.             num_sum += (left_sum + right_sum)
  22.            
  23.         except Exception as e:
  24.             #Если возникла ошибка при обработке строки, выводим строку и лог ошибки
  25.             print('Incorrect data format in this line:',
  26.                  line)
  27.             print('Error:',e)
  28.             break
  29.            
  30. #Проверка на пустой файл          
  31. if all_tickets_num == 0:
  32.     print('File is empty')
  33.    
  34. else:
  35.     #Вывод в файл
  36.     out = open('output.txt', 'w')
  37.     out.write( '{}\n{:.3}'.format( lucky_num, num_sum/(6*all_tickets_num) ))
  38.     out.close()
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement