Tamil Nadu HSE First Year Computer Science study materials
Tamil Nadu HSE First Year Computer Science study resources

Sentinel Loop for Class Averages

Tamil Nadu HSE First Year Computer Science · Put your knowledge to work in context.

All case studies

Sentinel Loop for Class Averages

A teacher wants one Python program to read marks for several students and print the class average. A mark of -1 is used as a sentinel to stop input. The teacher's draft is:

total = 0 count = 0 while True: m = int(input()) total = total + m count = count + 1 if m == -1: break avg = total / count print(avg)

The teacher tests it with the marks 40, 28, 65 and then -1. The printed average is 33.0, which looks too low. The teacher also wants a function that classifies the average as "A" for 75 and above, "B" for 60 to 74, "C" for 50 to 59, and "D" below 50, and wants to reuse the logic for three sections without copying the loop three times.

1. Trace the draft program with the inputs 40, 28, 65, -1. Show the final values of total and count, explain why the average is wrong, and give a corrected loop.

Read the worked answer

The loop adds every input before testing it. The running total is 40, then 68, then 133, then 132 because -1 is added. The count becomes 4. The program prints 132 ÷ 4 = 33.0. The correct average is 133 ÷ 3 = 44.33, because the sentinel is a stopping signal and not a mark. There are two defects: the sentinel is added to the total, and it is counted as a student. A corrected loop tests for -1 first, then validates the mark before adding:

while True: m = int(input()) if m == -1: break if 0 <= m <= 100: total += m count += 1

After the loop, check count > 0 before dividing, so an empty class cannot cause a division by zero.

2. Rewrite the solution using a function that takes the marks list and returns the average, and a second function that classifies the average. Explain how the functions exchange information.

Read the worked answer

A clean design separates calculation from classification:

def average(marks): if len(marks) == 0: return None return sum(marks) / len(marks)

def classify(avg): if avg >= 75: return "A" elif avg >= 60: return "B" elif avg >= 50: return "C" else: return "D"

The list is passed as an argument to average, and the computed float is returned to the caller. That returned value is then passed as an argument to classify, which returns a string. No global variable is needed. The elif chain matters: once avg >= 75 is true, the later tests are skipped, and the order from highest to lowest prevents a mark like 80 from being labelled B. The caller can reuse both functions for all three sections by passing different lists.

3. The teacher wants to avoid storing all marks in memory and still reuse the code. Describe how a sentinel-reading function could return both the total and the count, and explain one advantage and one risk of using a global variable instead.

Read the worked answer

A function can read until -1 and return a tuple: total and count. For example, read_marks() can keep local total and count variables, stop on -1, skip out-of-range marks, and then return total, count. The caller unpacks them with total, count = read_marks() and passes count to a guard before dividing. A tuple is suitable because the two results belong together and should not be modified accidentally. Using a global total and count instead would let the function update them directly without returning anything, which saves a line at the call site. The advantage is simplicity for a very small program. The risk is that any part of the program can change those names, so a second call may add to an old total, and debugging becomes harder because the function's effect is hidden. Returning values makes the data flow explicit.