What if you want to check several sensor readings? You could copy the same lines over and over, but changing all those copies would get tedious. A loop repeats a block for you. Let’s follow a small one, one repetition at a time.
Following each repetition¶
for sample in range(4): runs its block four times, assigning sample the
values 0, 1, 2, and 3. The stop value 4 is excluded. The loop variable changes
each time. A total you initialize before the loop can keep accumulating across
repetitions; initialize it inside and it starts over each time instead.
A while loop repeats as long as a boolean condition is true. For example,
while count < 3: requires something in its body to change count if it is to
finish. An accidental infinite loop can be stopped with Ctrl+C in the terminal.
We’ll mostly use bounded for loops for our early exercises.
Indentation matters here too. An indented print shows every repetition; an unindented print after the loop shows only the final state. Trace the values at the end of each repetition rather than trying to guess the whole loop at once.
Read it before running it¶
Put this worked example in student-work/foundations/loops.py.
Read it once and write down what you think it prints. Then save and run
python student-work/foundations/loops.py using your environment’s Python.
total = 0
for sample in range(4):
total = total + sample
print("Sample:", sample, "Total:", total)
print("Finished:", total)
The running totals are 0, 1, 3, and 6; the final line reports 6. Move the initialization inside the loop temporarily and compare: each repetition starts from zero, so you no longer accumulate a total. Restore it afterward.
Change range(4) to range(1, 5). This form starts at 1 and stops before 5.
Predict the new total. Later a robot framework will call our code repeatedly;
we won’t put a never-ending loop inside a callback that must return.
Try it yourself¶
A place to start
Initialize an accumulator once before the for loop. For the countdown, identify which value moves toward the stopping condition.
A more specific hint
Use range(1, 6) for the sum. In the while body, subtract 1 from the countdown after printing it.
Solution to Exercise 1
The final sum is 15. The countdown ends when its condition becomes false at zero; it does not need to print zero.
total = 0
for value in range(1, 6):
total = total + value
print(total)
print("Final total:", total)
count = 3
while count > 0:
print(count)
count = count - 1
Before moving on¶
Predict the effect of moving a print one indentation level left. Recall how a NameError differs from a loop that produces the wrong total.