# scores-list.py # David Kauchak # 10/10/2011 # # A set of functions for reading in scores and calculating # various statistics from the input scores. def get_scores(): """Read user input of numerical scores as floats into a list and return the list""" print "Enter the scores one at a time. Blank score finishes." scores = [] line = raw_input("Enter score: ") while line: scores.append(float(line)) line = raw_input("Enter score: ") return scores def inelegant_average(scores): """Calculate the average of the values in list scores""" sum = 0.0 count = 0 for score in scores: sum += score count += 1 return sum/count def average(scores): """Calculate the average of the values in list scores""" return float(sum(scores))/len(scores) def almost_median(scores): """Calculate the median of the values in list scores (almost)""" scores.sort() return scores[len(scores)/2] def assign_grades(scores): """Given a list of scores (numbers) calculate a letter grade for each score. If the score is greater than the average then it receives and A. If it is less than, an F. The function returns a list of grades where index i in the list corresponds to the score i in the input list""" avg = average(scores) grades = [] for score in scores: if score >= avg: # gets an A grades.append("A") else: grades.append("F") return grades def print_class_stats(scores): """Print out various stats about the list of scores""" print "Class max: " + str(max(scores)) print "Class min: " + str(min(scores)) print "Class average: " + str(average(scores)) print "Class median: " + str(almost_median(scores)) print "Class grades:" grades = assign_grades(scores) for i in range(len(grades)): print str(scores[i]) + " -> " + grades[i] # get the scores from the user and then print out the stats scores = get_scores() print "-"*10 print_class_stats(scores)