# The body of loops can contain any statements # A loop (outer loop) can contain another loop (inner loop). for i in range(5): print("i =", i) for j in range(3): print(" j =", j) print("done nested loop example (#1)") for i in range(5): print("i =", i) for j in range(100): if input(" Stop inner loop? ") == "yes": break print(" j =", j) print("done nested loop example, using break (#2)") # Write a function that takes a list of grades # Each item (in the list of grades) contains grades for each course # the student took. # The function returns a list of averages of the courses. # Example: # list_of_grades = [[80, 90], [90, 89, 97], [75, 90, 100], [70, 75, 90, 85]] # the function returns averages = [85.0, 92.0, 83.33, 80.0] def compute_averages(lst): """ The function takes a list of grades and returns a new list in which each item is the average of the grades for each course """ # get a list of grades of each course (i.e., each inner list) # compute its average as sum the grades divided by number of grades average = [] for course_grades in lst: total = 0 # keep track of total grades of each course for grade in course_grades: total += grade course_avg = total / len(course_grades) average.append(course_avg) return average list_of_grades = [[80, 90], [90, 89, 97], [75, 90, 100], [70, 75, 90, 85]] print(compute_averages(list_of_grades))