Back

Activity 1.21: Processing Lists

divider

Activity 1.21

Processing Lists

Key Concepts

The Accumulator Pattern

Adding Up a List

A variable that starts at 0 and grows a little on every pass through a loop is called an accumulator.

scores = [84, 91, 76, 88, 95]
total = 0
for score in scores:
total = total + score
print(total)

Adding Up a List

Terminal window
434

From Total to Average

Once you have a total, the average is one more line — divide by how many values there were.

average = total / len(scores)
print(average)

From Total to Average

Terminal window
86.8

Counting Instead of Summing

The same pattern counts things too — instead of adding each value, add 1 only when a condition is true.

count = 0
for score in scores:
if score >= 90:
count = count + 1
print(count)

Counting Instead of Summing

Terminal window
2

Today's Objectives

  • Summing a list with the accumulator pattern
  • Calculating an average from a total
  • Counting elements that meet a condition

Key Terms

Accumulator
A variable that starts at a base value and builds up a result across a loop, like a running total or a count.

'F' → Fullscreen

divider

Build

Create a new Python program named 1-21-processing-lists.


Task 1: Build a List and Total It

  • Ask how many scores to enter, collect them into a list with append(), then total them.
Processing Lists
count = int(input("How many scores? "))
scores = []
for i in range(count):
score = int(input("Enter a score: "))
scores.append(score)
total = 0
for score in scores:
total = total + score
print(f"Total: {total}")

Task 2: Calculate the Average

  • Divide the total by len(scores) to find the average.
Processing Lists
count = int(input("How many scores? "))
scores = []
for i in range(count):
score = int(input("Enter a score: "))
scores.append(score)
total = 0
for score in scores:
total = total + score
print(f"Total: {total}")
average = total / len(scores)
print(f"Average: {average}")

Task 3: Count the Passing Scores

  • Ask for a passing score, then count how many scores are at or above it.
Processing Lists
count = int(input("How many scores? "))
scores = []
for i in range(count):
score = int(input("Enter a score: "))
scores.append(score)
total = 0
for score in scores:
total = total + score
print(f"Total: {total}")
average = total / len(scores)
print(f"Average: {average}")
passing_score = int(input("Enter the passing score: "))
passing_count = 0
for score in scores:
if score >= passing_score:
passing_count = passing_count + 1
print(f"Scores at or above {passing_score}: {passing_count}")
divider

Checkpoint

Verify your program works correctly.

Example Output
How many scores? 5 [Enter]
Enter a score: 84 [Enter]
Enter a score: 91 [Enter]
Enter a score: 76 [Enter]
Enter a score: 88 [Enter]
Enter a score: 95 [Enter]
Total: 434
Average: 86.8
Enter the passing score: 80 [Enter]
Scores at or above 80: 3
divider

Reflection

Answer the following questions before submitting your work.

  1. What does the accumulator pattern actually do to the variable each time the loop runs?
  2. Why does calculating the average need total / len(scores) instead of just total?
  3. How does the counting accumulator in Task 3 differ from the summing accumulator in Task 1?
divider

Submit

Submit the required files to the appropriate dropbox.

Activity Complete