""" This is problem 8.11 on page 263 of Zelle's 'Python Programming...' text, namely : Write a program that accepts (from a file) a series of daily temps and computes degree-days : heating: number of degrees over 80 cooling: number of degress under 60 and outputs the two totals after all the data has been processed. See the "run" file that executes this. Jim M on Oct 13 2008 """ def get_data(filename): """Return a list of data from a file with numbers separated by spaces or newlines.""" data = [] try: infile = open(filename, 'r') except: print "Problem opening file." return data while True: line = infile.readline() if not line: return data number_strings = line.split() for number_string in number_strings: data.append(eval(number_string)) def user_filename(): """Prompt user for a filename.""" return raw_input("What is the filename? ") def heating_degreedays(data): """Return total of degrees over 80 from a list of degrees on consecutive days.""" heating = 0 hot_temp = 80 # print " heating ... " # debug for degree in data: # print " " + str(degree) # debug if degree > hot_temp: heating = heating + (degree - hot_temp) return heating def cooling_degreedays(data): """Return total of degrees over 80 from a list of degrees on consecutive days.""" cooling = 0 cold_temp = 60 for degree in data: if degree < cold_temp: cooling = cooling + (cold_temp - degree) return cooling def main(): print "problem 8.11 on page 263" print data = get_data(user_filename()) # print "data = " + str(data) # debug print "Heating degree-days = %i" % heating_degreedays(data) print "Cooling degree-days = %i" % cooling_degreedays(data) main()