Repeating and Remembering

Recap and Feedback


In this part, you learned to treat strings as sequences, repeat work with for, store several values in lists, process and change lists, and collect an unknown amount of input with while True and break.

The central idea is matching repetition to its job:

  • use direct iteration when every existing item should be processed;
  • use indexes when positions are needed;
  • use range(...) when the repetition count is known;
  • use while True with break when an interaction decides when to stop.

Putting the Patterns Together

Larger programs often become easier to reason about when they are divided into phases. One loop can collect raw data, another can select the usable values, and a final loop can summarize them. Between phases, check what the lists contain and whether any of them are empty.

The summary must use only the values that passed the filter. For example, if the measurements are 4, -2, and 0, and negative measurements are invalid, the usable values are 4 and 0. Their total is 4, and their average is 2. There are three entered measurements but only two valid ones.

A filtered list can be empty even when the original list contains several measurements. Then there is no average to calculate. Dividing by the length of that empty list would fail, so the program needs to handle this case separately.

The rainfall exercise applies that design to a familiar kind of measurement. It contains the full program requirements; use the patterns from the earlier chapters to decide what belongs in each phase.

Analyze rainfall measurements

0 / 30 points

Write a program that records, filters, and summarizes rainfall measurements.

Read input lines without visible prompts. You may assume each line can be converted to a number. The numeric value 9999 stops input and is not a measurement. Equivalent numeric forms, such as 9999.0, must also stop input.

Complete the program in three phases:

  1. Collect every measurement in a list, including negative measurements, until the user enters the sentinel.
  2. Build a second list containing the valid measurements from the first list. Negative measurements are invalid; zero is valid rainfall.
  3. Calculate the total and average of the valid measurements. If there are none, the total is zero and the average is the text none.

Write an explicit loop for each phase and calculate the total yourself; do not use sum(...). Keep the original measurements so that you can report both how many were entered and how many were valid.

For this input:

5.5
-2
0
6.5
9999

print:

Measurements entered: 4
Valid readings: 3
Total rainfall: 12.0
Average rainfall: 4.0

If the input contains only -2, -4, and then 9999, print:

Measurements entered: 2
Valid readings: 0
Total rainfall: 0
Average rainfall: none

Use the four labels exactly as shown. Print the ordinary result of Python’s arithmetic without rounding it. Equivalent numeric spellings such as 0 and 0.0 are accepted.

Test immediate stopping, only negative measurements, zero rainfall, decimal measurements, and a sentinel written as 9999.0.

Compare Finished Designs

Two programs can produce the same result while storing different values. Compare what each program remembers, how it handles the case with no valid measurements, and when each design might be useful.

Review two rainfall-analysis designs

0 / 10 points

Two programmers solve a simplified version of the rainfall task: their programs print only the average, or No average when no valid measurements exist. In both versions, 9999 ends input, negative measurements do not contribute to the summary, and zero is a valid measurement.

Version A

measurements = []
while True:
measurement = float(input())
if measurement == 9999:
break
measurements.append(measurement)
valid_measurements = []
for measurement in measurements:
if measurement >= 0:
valid_measurements.append(measurement)
total = 0
for measurement in valid_measurements:
total = total + measurement
if len(valid_measurements) == 0:
print("No average")
else:
print(total / len(valid_measurements))

Version B

valid_count = 0
total = 0
while True:
measurement = float(input())
if measurement == 9999:
break
if measurement >= 0:
valid_count = valid_count + 1
total = total + measurement
if valid_count == 0:
print("No average")
else:
print(total / valid_count)

In a short response, compare the two versions. Explain what state each version keeps, how both treat the sentinel and the case with no valid measurements, and one practical advantage of each design. Base the comparison on the code rather than merely listing syntax names.

Check Your Understanding

  1. What do strings and lists have in common as sequences?
  2. When does a loop need an index rather than only the current value?
  3. What is the purpose of break in a sentinel loop?
  4. How can filtering a nonempty list produce an empty list?
  5. Why should immediate stopping and empty-list behavior be considered deliberately?

Feedback

Please reflect on whether the explanations and exercises supported the learning objectives, whether the workload and difficulty felt appropriate, and what should be improved. The form also asks for your estimated study time and use of generative AI tools.

Reflection and feedback

0 / 5 points

Consider the following statements and answer them based on your experiences from studying the materials and working on the assignments in this part. When answering the numeric statements, answer them so that value 1 means that you strongly disagree with the statement, and value 7 means that you strongly agree with the statement. Please be as honest as possible when answering. The feedback will be used to improve the course.

Looking Ahead

Programs with several phases can grow into one long block of code. In Part 3, you will learn how functions divide a program into smaller pieces with clear responsibilities. You will also learn tools for responding safely when input cannot be converted to the expected type.