Repeated work — quick review
Read a for loop
A ticket checker at a gate takes the tickets of the people in line one at a time, from the front, and does the same check on each.
A for loop over a sequence runs its indented body once for each item, in order, unless a break ends it sooner. Before each pass, the name after for is given the next item. When the items run out, the program carries on with the first line after the body. An empty sequence gives no passes.
Find the sequence after
inand write its items in order. A string gives one item for each character.Find the body: the lines indented under the
forline.For each item, write the loop name's value, then run the body's lines in order with that value.
After the last pass, move to the first line after the body. In this unit's programs that is the next line indented no further than the
forline.Count the passes: one for each item, unless a
breakends the loop sooner. Check that the output holds what the body displays once for each pass.
A line meant to run once can slip into the body. If print("done") were indented under the for line, it would run on every pass and the output would hold done three times. Check the indentation of each line below a for line.
The loop name keeps its last value after the loop ends. After the loop over "MAP", letter still holds P. Use the loop name inside the body, where it holds the current item.
Choose the bounds of range
A row of seats is numbered from a first seat up to a sign that says where the row ends.
range(stop) gives the whole numbers from 0 up to but not including stop. range(start, stop) starts at start when start is below stop, and is empty otherwise. range(start, stop, step) moves by step each time and holds the numbers below stop for a positive step, or above stop for a negative one. stop is never included, and a step of 0 raises a ValueError.
Write the first number you want. That is
start. It can be left out when it is 0 and no step is given, because a step needs a start before it.Choose the step. Leave it out to count up by 1, write a bigger number as the third number to skip, or a negative one to count down.
Write the last number you want, and check that counting from
startby the step lands on it; if it does not, change the start or the step. Then setstoppast it, no more than one step beyond it: above it when counting up, below it when counting down. For a count up by 1,stopis the last number plus 1. Inrange(10, 0, -3)the last number is 1, and the stop 0 lies below it, within one step of 3.Check the count: for a step of 1 and a
stopabovestart,range(start, stop)givesstopminusstartnumbers.Write out the numbers the range gives, or note that it gives none, and compare them with what you wanted.
The usual slip is a stop one short. range(1, 5) ends at 4, so a loop meant to count to 5 needs range(1, 6). Write out the last number of the range before you run the program.
Counting down needs a negative step. range(5, 1) holds no numbers, because its step is 1 and 5 is not below 1. The loop makes no passes and nothing is displayed. Write range(5, 1, -1) for 5, 4, 3 and 2.
Trace a while loop
Someone filling a bucket from a tap looks at it before each scoop: if it is not full yet, one more scoop goes in, and then they look again.
A while loop tests its condition before every pass. While the condition is true, it runs the indented body and, when the body reaches its end, goes back to the test; a break ends it sooner. When the condition is false, the program carries on with the first line after the body, and a condition that is false at the first test gives no passes.
Write each name the condition uses, with its value before the loop.
Test the condition with those values. If it is false, skip to the first line after the body.
If it is true, run the body's lines in order and write each name's new value.
Find what can make the condition false at a later test: a line in the body that changes a name it reads, or a new value it reads each time, such as a reply from
input(). If nothing can, and nothing else ends the loop, such as thebreakof this unit's last lesson, the loop will not end on its own.Go back to the test with the new values, and repeat until a test is false or something ends the loop sooner.
Leave out the line that changes the name, and the condition never becomes false. A countdown without seconds = seconds - 1 displays 3 again and again and never ends. Press Control-C, the interrupt key, to stop it; Python stops the program with a KeyboardInterrupt.
The other slip is the edge of the condition. seconds > 0 stops before 0 is displayed, and seconds >= 0 displays 0 as well. Trace the last two tests to see which values get a pass.
Keep a running total
Picture a cashier starting a sale at 0.
A running total or a count is set to its starting value before the loop and changed inside the body, so each pass builds on the last. The biggest value so far starts with the first value and is replaced inside the loop when an if finds a bigger one. x += 1 adds 1 to x.
Decide what to keep: a total, a count or the biggest value.
Before the loop, give it a starting value: 0 for a total or a count, the first value for the biggest. A line such as
total = total + stepsreads the name before it changes it, so the name needs a value first.Inside the body, change it on the passes that need it: add each value to a total, add 1 when a pass matches, or replace the biggest when an
iffinds a bigger value.After the loop, use the result: display it, or, when the count is above 0, divide the total by the count for an average.
Trace two passes by hand and check that the value after each pass builds on the one before.
A total set to 0 at the top of the body starts again on every pass. With total = 0 as the body's first line, the step program would display Total: 4000, the last day's steps alone, and Average: 1000.0, that one day divided by 4. Put the starting value above the for line.
Starting the biggest value at 0 goes wrong when every value is below 0: no value is bigger than 0, so the program would display 0, a number that was never read. Start with the first value instead, as the worked example does.
Stop a search at a match
Someone looking for keys opens one drawer after another.
A break line ends the nearest loop around it at once, skips the rest of that pass and every later pass, and the program carries on with the first line after the loop. After a break, a for loop's name keeps its current item.
Above the loop, set any name that the program reads after the loop and that only a match changes, such as
found = False.Write the test that marks a match, and put it in an
ifinside the body.Inside that
if, do what the match needs, such as settingfound = True, and then writebreak.Trace the passes until the test is true. At the
break, jump to the first line after the loop.Check the case with no match. A for loop runs to its end, and a name that only a match changes, such as
found, still holds the value it was given above the loop. Awhile True:loop that reads input and meets no match keeps reading; when the input runs out,input()stops the program with an EOFError.
A message about a miss written in the body after the match test runs on every pass that misses: in a search for a space, once for each character before the space, or once for every character of a string with no space. Decide after the loop, with a name such as found, whether the search found nothing.
In a while True: loop, the test never ends the loop. If the test before the break is never true for the replies typed, as when each reply is compared with "Done" and the person types done each time, the loop keeps asking. It stops only with an error: an EOFError when the input runs out, or a KeyboardInterrupt when you press Control-C, the interrupt key.