Respuesta :
Answer:
Python code:
#main method
def main():
#variable to store sum of the numbers
addition = 0.0
#variable to store count of numbers
n = 0
#opens the file numbers.dat as a random file
with open('numbers.dat') as numbers_file:
#for loop to read data from the file
for line in numbers_file:
#adding the line into the variable
addition+=int(line)
#increment the variable n
n+=1
#displays the average of numbers stored in the file
print("average of all the numbers stored in 'numbers.dat' is:",format((addition/n),'.2f'))
main()
Explanation:
To find the average of integers stored in the file “numbers.dat”. A file is created and some integer values are stored in it.
Note: The data stored in the file is shown in the output section.
In the python program, firstly the main method is defined and the variables are initialized as per the requirement (the use of each variable is shown as comments in the program).
The with open() function is used to open the file “numbers.dat”. And the for-loop is used to read the numbers line by line.
The print() method is used to display the average of the numbers.
Output:
