# # Author: James Tam # Version: October 29, 2013 (Initialization occurs in a separate function) # Version: October 26, 2011 (Port to Python V3.X) # # The second version of a program that tracks the grades for a class. # # Global constant to specify the size of the list. CLASS_SIZE = 5 # Creates a list with 5 elements. # Each element is set to (an invalid) default grade def initialize(): classGrades = [] # Create a list with 5 elements with each element initialized to zero # Within the body of the loop create a new list element that contains 0 # and append / add it to the end of the list. for i in range (0, CLASS_SIZE, 1): classGrades.append(-1) return(classGrades) # Step through the list an element at a time and get the user to input the # grades. def read(classGrades): total = 0 average = 0 for i in range (0, CLASS_SIZE, 1): # Because list indices start at zero add one to the student number. temp = i + 1 print("Enter grade for student no.", temp, ":") classGrades[i] = float(input (">")) total = total + classGrades[i] average = total / CLASS_SIZE return(classGrades, average) # Traverse the list and display the grades. def display(classGrades, average): print() print("GRADES") print("The average grade is %.2f%%" %average) for i in range (0, CLASS_SIZE, 1): # Because list indices start at zero add one to the student number. temp = i + 1 print("Student No. %d: %.2f%%" %(temp,classGrades[i])) # PROGRAM BEGINS EXECUTION HERE def start(): classGrades = initialize() classGrades, average = read(classGrades) display(classGrades,average) start()