Week 5 · Session 3

Loops

Doing something more than once without writing it more than once.

Lab 2 Wrote This Three Times

The CRITICAL branch flashes the light three times.

  • led.on(), time.sleep(0.1), led.off(), time.sleep(0.1), and then the same four lines again, and again
  • It works. It is also twelve lines that say one thing: flash, three times
  • Make the warning flash ten times and you are copying forty lines. A loop says it once

The for Loop

  • for flash in range(3): means: run the indented block below, three times
  • The block is everything indented under the for line, the same rule as if
  • One pass through the block is an iteration. range(3) is three iterations
  • The first unindented line after the block runs once, after the last iteration

Try It: Flash Three Times

This is the CRITICAL branch as a loop, with print standing in for the light.

  • Run it. Then change 3 to 10 and run it again
  • Add print("Warning over.") at the bottom with no indentation, and see how many times it prints
  • Indent that same line under the loop and run it once more

              

What range Does

  • range(3) makes the loop go through 0, then 1, then 2. It stops before 3
  • range(1, 4) starts at 1 and goes through 1, 2, 3. The stop value is not included
  • The name after for holds the current value. Use it inside the block, or ignore it and just count trips

Try It: Trace First, Then Run

Write down what this prints before you press Run.

  • How many lines print, and what is on each one
  • Then change range(1, 4) to range(4) and predict again before running

              

An if Inside a Loop

The question is asked once per trip, about the value the loop handed over.

  • flash % 3 == 0 is True for 3, 6 and 9: the remainder is what picks out every third one
  • Counting from 1 is what makes "every third" line up with the remainder
  • A counter set to 0 before the loop, plus one inside the if, ends up holding how many trips took that branch

Try It: Every Third One Long

Run it. Which flash numbers are held long, and why those?

  • Change range(1, 8) to range(7) and run again. Which flashes are long now, and why did that move?
  • Add a counter: set long = 0 before the loop and long = long + 1 inside the if, then print it after the loop

              

Try It: A Loop Inside a Loop

Read it before you run it. Count the lines it will print.

  • The inner for runs all the way through on every trip of the outer one
  • Two attempts, and each attempt prints one header, three short and three long. Check your count against the output
  • Indentation decides which loop a line belongs to. Un-indent the last for and run again

              

while: Repeat Until Something Changes

For when you do not know the count in advance.

  • while charge < 40: checks the condition before every trip and stops the first time it is False
  • Something inside the block has to move the condition toward False, or the loop never ends
  • Run it. Then change the starting charge to 50 and see how many trips happen

              

Which Loop, When

  • for ... in range(n) when you know how many times: flash three times
  • while when you stop on a condition, not a count: keep charging until the battery is ready
  • An if inside a loop decides each trip on its own. A loop inside a loop repeats a repetition
  • Lab 3 asks for each of these once, on one lantern
← Exit
1 / 11