# Grades2 Example # Source code file: grades2.py # Input file: grades.txt # Compute the average of all the grades. # To compute the average, we keep track of # the sum and the count of the grades. sum = 0 count = 0 fin = open("grades.txt", "r") # Read first line from file line = fin.readline( ) # Process lines until end of file while line != "": grade = int(line.strip( )) sum = sum + grade count = count + 1 line = fin.readline( ) print(sum, count) print("Average of grades:", round(sum / count, 2)) fin.close( ) # Output: # Average of grades: 88.88