Ask your own question, for FREE!
Computer Science 12 Online
OpenStudy (immanuelv):

I have a python problem

OpenStudy (immanuelv):

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 months with the highest and lowest amounts. The input data is available in Program9.txt 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 4 functions. this i the read file program9.txt 1.89 1.99 2.14 2.51 5.03 3.81 1.97 2.31 2.91 3.97 2.68 2.44 so far i have 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()

OpenStudy (immanuelv):

@abtster

OpenStudy (e.mccormick):

The inFile.readline() reads in an entire line. Then you parse it to words, which makes no sense to me. See, the input file you listed has 12 lines with a number on each line. So why would you need to split it? As you read in each line you can check for if the value is the higest or the lowest. On the first line read you might want to set the highest and the lowest. Then it is a simple comparison and replace from that point on. At the same time you can do a running total to get the annual rain fall and divide that by 12.0 to get the average.

OpenStudy (rsmith6559):

BTW, according to your description of the program, you can't read the file in main().

OpenStudy (immanuelv):

def rainFall(): with open('program9.txt') as inputs: monthRain = [float(line) for line in inputs] global total jan, feb, mar, apr, may, jun, jul, aug, sep, oct, nov, dec = range(12) month = ['January', 'February', 'March ', 'April ', 'May ', 'June ', 'July ', 'August ', 'September' , 'October', 'November', 'December'] print("Month \t\t\t Amount") print("----- \t\t\t ------") for rain in range(len(month)): print(month[rain],"\t\t",monthRain[rain]) print("-------------------------------") totalRain = totalRainfall(monthRain) averageRain = averageRainfall(monthRain) maxRain = maxRainfall(monthRain) def totalRainfall(rain): total = sum(rain) print("Total Rainfall :\t", format(total, '.2f')) def averageRainfall(rain): average = float(sum(rain)/len(rain)) print("Average Rainfall :\t", format(average, '.2f')) def main(): monthlyRain = rainFall() main() the problem i have right now is with finding max rain and min rain i just cant figure out how to display the month next to the amount this is my code right now for max rain and min rain def maxRainfall(rain): maxRain = max(rain) month = print("Max Rainfall in" month, "amount", maxRain)

OpenStudy (e.mccormick):

use the monthRain.index() method for the highest number in that list as how you decide what month name to print.

OpenStudy (immanuelv):

something like this work? @e.mccormick def maxRainfall(rain, months): maxRain = max(rain) maxMonth = rain.index(maxRain) print("Max Rainfall in Month",maxMonth+1, "amount", maxRain) this just shows month number though :(

OpenStudy (e.mccormick):

You have an index number. Now you can get the month name.

OpenStudy (immanuelv):

def maxRainfall(rain, months): maxRain = max(rain) maxMonth = rain.index(maxRain) mxMonth = months[maxMonth] print("Max Rainfall in Month",mxMonth, "amount", maxRain) this is what i got! thanks mate!!! you are the best!

OpenStudy (e.mccormick):

Yep, that is is. Use the number to get the index key to get the word. =)

OpenStudy (anonymous):

how exactly did you get the month to display next to the amount? I ask because Im having the same issue with this problem, my work is exactly identical to yours however, I get this error "maxRainfall() missing 1 required positional argument: 'month' ", but heres the function, def maxRainfall(rain, month): maxRain = max(rain) maxMonth = rain.index(maxRain) w = month[maxMonth] print("Max Rainfall", maxRain, w)

OpenStudy (e.mccormick):

How are you calling it?

OpenStudy (anonymous):

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 def rainFall(): with open('program9.txt') as inputs: monthRain = [float(line) for line in inputs] global total jan, feb, mar, apr, may, jun, jul, aug, sep, oct, nov, dec = range(12) month = ['January', 'February', 'March ', 'April ', 'May ', 'June ', 'July ', 'August ', 'September' , 'October', 'November', 'December'] print("Month \t\t\t Amount") print("----- \t\t\t ------") for rain in range(len(month)): print(month[rain],"\t\t",monthRain[rain]) print("-------------------------------") totalRain = totalRainfall(monthRain) averageRain = averageRainfall(monthRain) maxRain = maxRainfall(monthRain) minRain = minRainfall(monthRain) def totalRainfall(rain): total = sum(rain) print("Total Rainfall :\t", format(total, '.2f')) def averageRainfall(rain): average = float(sum(rain)/len(rain)) print("Average Rainfall :\t", format(average, '.2f')) def maxRainfall(rain, month): maxRain = max(rain) maxMonth = rain.index(maxRain) month = [maxMonth] w = month(); print("Max Rainfall", maxRain, w) def minRainfall(rain): minRain = min(rain) minMonth = rain.index(minRain) print("Min Rainfall ", minRain,minMonth) def main(): monthlyRain = rainFall() # Call the main function. main()

OpenStudy (anonymous):

thats what I have so far, and everything works except for the name of the month

OpenStudy (anonymous):

1.89 1.99 2.14 2.51 5.03 3.81 1.97 2.31 2.91 3.97 2.68 2.44

OpenStudy (anonymous):

thats the txt

OpenStudy (e.mccormick):

maxRain = maxRainfall(monthRain) but def maxRainfall(rain, month): Called with 1, but takes 2.

Can't find your answer? Make a FREE account and ask your own questions, OR help others and earn volunteer hours!

Join our real-time social learning platform and learn together with your friends!
Can't find your answer? Make a FREE account and ask your own questions, OR help others and earn volunteer hours!

Join our real-time social learning platform and learn together with your friends!