113k views
0 votes
Assume a file containing a series of integers is named numbers.txt and exists on the computers disk. Write a program that calculates the average of all the numbers stored on the file. Write this in Python

1 Answer

7 votes

Answer:Here is one way to calculate the average of all the numbers stored in a file named "numbers.txt" in Python:

Step-by-step explanation:

# Open the file for reading

with open("numbers.txt", "r") as file:

# Read all the lines in the file

lines = file.readlines()

# Convert each line to an integer

numbers = [int(line.strip()) for line in lines]

# Calculate the sum of the numbers

total = sum(numbers)

# Calculate the average by dividing the total by the number of numbers

average = total / len(numbers)

# Print the result

print("The average of the numbers is", average)

User Ilo
by
8.7k points