12.9k views
5 votes
Write a program that calculates the average rainfall for three months. The program should ask the user to enter the name of a data file that that contains rainfall data for three months. The file consists of three lines, each with the name of a month, followed by one or more spaces, followed by the rainfall for that month. The program opens the file, reads its contents and then displays a message like:

1 Answer

5 votes

Answer:

Step-by-step explanation:

The following Python program reads the file called text.txt and loops through each line, removing whitespace and seperating the month and rainfall. Then it prints each Month with its corresponding rainfall amount. Finally it calculates the average rainfall and outputs that.

file = open('text.txt', 'r')

total_rainfall = 0

for line in file:

line = line.replace('\\', '')

info = line.split(' ')

info = [i for i in info if i != '']

print(info[0] + " will have a total of " + info[1] + " inches of rainfall.")

total_rainfall += int(info[1])

average = total_rainfall / 3

print("Average Rainfall will be " + str(average) + " inches")

Write a program that calculates the average rainfall for three months. The program-example-1
User Engnyl
by
4.9k points