# scores-list.py # A set of functions for reading in scores and calculating # various statistics from the input scores. def get_scores(): """ Reads user input of numerical scores as floats into a list and returns the list :return: None """ print("Enter the scores one at a time. Blank score finishes.") scores = [] line = input("Enter score: ") while line != "": scores.append(float(line)) line = input("Enter score: ") return scores def inelegant_average(scores): """ Calculates the average of the values in list scores in an inelegant way :param scores: (list) a list of numbers that correspond to scores :return: (float) the average of the values in scores """ sum = 0.0 count = 0 for score in scores: sum += score count += 1 return sum / count def average(scores): """ Calculates the average of the values in list scores in an elegant way :param scores: (list) a list of numbers that correspond to scores :return: (float) the average of the values in scores """ return sum(scores) / len(scores) def median(scores): """ Calculates the median (the middle number in a sorted list) of the values in list scores :param scores: (list) a list of numbers that correspond to scores :return: (float) the median of the values in scores """ # start by sorting the list scores.sort() # find the mid-point midpoint = int(len(scores) / 2) if len(scores) % 2 == 0: # even number of elements, the median is # the average of the middle pair return (scores[midpoint - 1] + scores[midpoint]) / 2 else: # if the list has an odd number of elements, the median is # the number in the middle return scores[midpoint] def assign_grades(scores): """ Calculates a letter grade for each score in a given a list of scores (numbers). If the score is greater than the average, then it receives and A. If it is less than the average, then it receives an F. The function returns a list of grades where index i in the list corresponds to the score i in the input list :param scores: (list) a list of numbers that correspond to scores :return: (list) a list of grades (str) that correspond to each of the given scores """ # calculate the average of all provided scores avg = average(scores) # make a new (empty) list grades = [] # for each of the provided scores for score in scores: # if the score is greater than the average if score >= avg: # it receives an A grades.append("A") else: # otherwise, an F grades.append("F") return grades def print_class_stats(scores): """ Prints out various stats about the list of scores :param scores: (list) a list of numbers that correspond to scores :return: None """ # prints stats about the input 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(median(scores))) print("Class grades:") grades = assign_grades(scores) for i in range(len(grades)): print(str(scores[i]) + " -> " + grades[i]) def main(): # get the scores from the user and then print out the stats scores = get_scores() print("-" * 10) print_class_stats(scores) if __name__ == "__main__": main()