Задача

2/7

Reading data from a file

Теория

Reading data from a file

When reading a text file, the stream of bytes enters the program input sequentially one after the other, so the file provides sequential access to data. That is, if we need to read the 10th value from the file, we must first read the previous 9.

Reading a single line of a file allows the readline() method to execute. This method is called on a file variable. Fin = open("input.txt") s = Fin.readline()
Various methods can be applied to the read string, similar to those used when reading from the keyboard (split(), map(), etc.). For example, if there are two numbers separated by a space in a line of a file, then you can count them as follows: Fin = open("input.txt") s = Fin.readline().split() # split line on spaces s = ["2007", "2021"] a, b = map(int, s)         # apply the int() method to all elements of the list s,   # that is, convert the character string to a number # a, b = int(s[0], s[1])   # this is the same as the line above # a, b = [int(x) for x in s] # same as a generator
The read() method reads the entire contents of the file and returns a string that may contain the characters '\n'. If an integer parameter is passed to the read() method, no more than the specified number of characters will be read. For example, you can read a file byte by byte using the read(1).
method.
When a file is opened, the pointer that determines the current location in the file is set to the beginning of the file and, when read, is shifted to the position following the data read. When writing, the pointer is moved to the next free position.

Задача

The file stores one string of integers. Each number is separated from the other by a single space. Find the sum and the arithmetic mean of the given numbers.
In your answer, write two numbers separated by one space, first the sum of the numbers, then their arithmetic mean. 

For example: 123 456.7

Выберите правильный ответ, либо введите его в поле ввода

Комментарий учителя