I am unsure of how to fix this. I attached a picture of the feedback from the teacher. COSC 1336 – Programming Fundamentals I Program 9 – Functions, Lists, and Strings Write a program that read the total rainfall for each of the 12 months into a list. The program should calculate and display the total rainfall for the year, the average monthly rainfall, and the name of the months with the highest and lowest amounts. The input data is available in Program9.txt file. No input, processing, or output should happen in the main function. All work should be delegated to other functions. The program should have at least 5 functions (main and developerInfo included). Include the recommended minimum documentation for each function. See the program one template for more details. Do not use any global variables. You will not get credit for the program if you do. Run your program with the input file, Program9.txt. Create a folder named, fullname_program9. Program9.txt input file information
I am unsure of how to fix this. I attached a picture of the feedback from the teacher.
COSC 1336 – Programming Fundamentals I Program 9 – Functions, Lists, and Strings
Write a program that read the total rainfall for each of the 12 months into a list. The program should calculate and display the total rainfall for the year, the average monthly rainfall, and the name of the months with the highest and lowest amounts. The input data is available in Program9.txt file.
No input, processing, or output should happen in the main function. All work should be delegated to other functions. The program should have at least 5 functions (main and developerInfo included). Include the recommended minimum documentation for each function. See the program one template for more details.
Do not use any global variables. You will not get credit for the program if you do.
Run your program with the input file, Program9.txt. Create a folder named, fullname_program9.
Program9.txt input file information
1.89
1.99
2.14
2.51
5.03
3.81
1.97
2.31
2.91
3.97
2.68
2.44
The code I have
# Main function
def main():
developerInfo()
# get rainfall info using getData function
rainfall = pull_data()
# get the total rainfall for year using totalRainfallOfTheYear function
total_rainfall = total_rain_year(rainfall)
# get the average monthly rainfall using averageMonthlyRainfall function
avg = average_monthly_rain(rainfall)
# print the total rainfall for the year
print("Total rainfall for the year: ", float("{0:.2f}".format(total_rainfall)))
# print the average monthly rainfall
print("The average monthly rainfall is: ", float("{0:.2f}".format(avg)))
# print the month with highest rainfall using highestRainfall function
print("Month with highest rainfall is: " + highest_monthly_rain(rainfall))
# print the month with lowest rainfall using lowestRainfall function
print("Month with lowest rainfall is: " + lowest_rainfall(rainfall))
#Function that reads the rainfall data from
#the text file program9.txt
def pull_data():
# open the text file
inFile = open('program9.txt', 'r')
#read the first record
lineRead = inFile.readline()
#while more records exsist
while lineRead != '':
# split records into substrings
words = lineRead.split()
# Read next record
lineRead = inFile.readline()
# close file
inFile.close()
return words
# This function returns the total yearly rainfall
def total_rain_year(rainfall):
total = 0.0
# iterate through the rainfall list
for i in rainfall:
# add each data to total
total = total + (float)(i)
# return the total rainfall
return total
# This function calculates and returns the average monthly rainfall
def average_monthly_rain(rainfall):
# get the total rainfall for the year
total = total_rain_year(rainfall)
# calculate and return the average rainfall
avg = total / len(rainfall)
return avg
# Function that returns the month with highest rainfall
def highest_monthly_rain(rainfall):
# store the month names in a list
months = ["Jan", "Feb", "Mar", "Apr", "May", "Jun", "Jul", "Aug", "Sep", "Oct", "Nov", "Dec"]
index = 0
# assume the month 1 has the highest rainfall
highest = rainfall[0]
highest_index = index
# iterate through the rainfall list
while index < len(rainfall):
# compare and get the index of the month with highest rainfall
if(rainfall[index] > highest):
highest_index = index
highest = rainfall[index]
index = index + 1
# return the month name using the index of the month with highest rainfall
return months[highest_index]
# Function that returns the month with lowest rainfall
def lowest_rainfall(rainfall):
# store month names in a list
months = ["Jan", "Feb", "Mar", "Apr", "May", "Jun", "Jul", "Aug", "Sep", "Oct", "Nov", "Dec"]
index = 0
# assume month 1 has the lowest rainfall
lowest = rainfall[0]
lowest_index = index
# iterate through the list
while index < len(rainfall):
# compare and get index of month with lowest rainfall
if(rainfall[index] < lowest):
lowest_index = index
lowest = rainfall[index]
index = index + 1
# return month name using index of month with lowest rainfall
return months[lowest_index]
#End of the main function
# Call the main function
main()
#End of program 9
_______________________________________________________________________
This is the template that must be used in the code
def main():
inFile = open('program9.txt', 'r')
lineRead = inFile.readline() # Read first record
while lineRead != '': # While there are more records
words = lineRead.split() # Split the records into substrings
annualRainfall = float(words[0])
print(format(annualRainfall, '.2f'))
lineRead = inFile.readline() # Read next record
# Close the file.
inFile.close() # Close file
# Call the main function.
main()
Step by step
Solved in 2 steps with 1 images