# Lab 7, CS0008, Fall 2013 # # Files and 2-dimensional arrays # # Specification of the input file: # # The file has 3 header lines. # After that, each line contains the average monthly temperatures # for a year (separated by spaces) starting with January. # # Use nested lists to represent the temperatures. # Each element of the list is a list of 12 measurements, one for each month. # # Let's use a smaller example: suppose we have only 4 months, and the data # is the following: # #24.7 25.7 30.6 47.5 #16.1 19.1 24.2 45.4 #10.4 21.6 37.4 44.7 # # # Your list will be: # [[24.7,25.7,30.6,47.5],[16.1,19.1,24.2,45.4],[10.4,21.6,37.4,44.7]] # # We'll number the years 0 to 2, and the months 0 to 3. # # temperature[2][3] is 44.7, the temperature for year 2, month 3. # # This is like using 2-dimensional arrays in other languages, # an important thing to know. # # Here are the names of the months typed in for you: # 'Jan', 'Feb', 'Mar', 'Apr', 'May', 'June', 'July', 'Aug', 'Sept', 'Oct', 'Nov', 'Dec' # # Note: a couple of the functions are almost the same. None of them are very large. # When possible one function should call another. # def inputTemperatureData(filename): '''Open the specified filename, read past the three-line header, read in the temperature data on the file, and close the file. Return the temperature data in the form of a list of lists, where each list element is a list of 12 temperature readings.''' # Hint: use readline to read the first three lines, and then # a while loop to read the numbers. See read_sales.py.txt # on the schedule for 10/7 and the Python Shell for 10/9. # # Hint: you need to convert the input from strings to # to floats. def printDataPoint(tempData,mo,yr): '''tempData is the tempData is the data structure returned by inputTemperatureData. mo is an integer in the range 0 to 11. yr is in the range 0 to len(tempData) - 1. Print the temperature for that month and that year. Suppose mo is 1 and yr is 3. The output should look like this (assuming the value is 65.7): The ave. monthly temperature during Feb in the 4th year is 65.7''' # Do not use a big if - elif - statement! def avgTempMarch(tempData): '''Return the average temperature for the month of March as a float for all years in tempData. tempData is the data structure returned by inputTemperatureData.''' def avgTemp(tempData, mo): '''Return the average temperature for month mo as a float for all years in temp_data, where mo is an integer between 0 and 11, representing January to December, respectively. tempData is the data structure returned by inputTemperatureData.''' def higherAvgTemp(tempData, mo1, mo2): '''Return either mo1 or mo2 (integers representing months in the range 0 to 11), whichever has the higher average temperature for all years in tempData. If the months have the same average temperature, then return -1. tempData is the data structure returned by inputTemperatureData.''' def avgTemps (tempData): ''' Return the average of all the temperatures sotred in tempData. tempData is the data structure returned by inputTemperatureData.''' def grade(myAns,correctAnswer): if myAns == correctAnswer: print("Correct, the answer is",myAns) else: print("My answer is",myAns,"but the correct answer is",correctAnswer) def main(): tempData = input_temperature_data("lab7.dat.txt") grade(avgTempMarch(tempData),32.475) grade(avgTemp(tempData,3),46.525) grade(higherAvgTemp(tempData,2,3),3) printDataPoint(tempData,mo=5,yr=2) main()