CSwithZain0 / 28passed
Block C · steps 17 to 28

Loops that count, and loops inside loops

The idea

A loop is one piece of code, run again and again, with a counter holding a different value each time.

You do not write the work out ten times. You write it once and tell Python how many times to run it. The counter is an ordinary variable: print it, add it up, or use it to pick an item out of a list.

Trace it before you type. Most loop mistakes are obvious in ten seconds on paper and invisible on screen for an hour. So trace this one first.

Step through it start

      
i
·
Passes
0
Press Step. Watch what i is, and count how many times the print line actually runs.
Output

        

Three passes, not four. range(1, 4) stops before the 4.

Reference

Every form of range, and what it gives you

You writeThe counter takesPasses
range(1, 4)1, 2, 33
range(1, 11)1 … 1010
range(3)0, 1, 23
range(10, 0, -1)10 … 110

Counting down in pseudocode is FOR i ← 10 TO 1 STEP -1 ... NEXT i. In Python it is range(10, 0, -1).

The pseudocode you already know includes its last value. FOR i ← 1 TO 10 runs with i = 10. Python's second number is where it stops, so the same loop is range(1, 11). One character, and it is the most expensive one in this block.

One number on its own starts at zero. range(3) gives 0, 1, 2. That feels wrong today and it will feel normal next month, because every list you touch starts at 0 as well.

The rule with no word for it

The indentation is the NEXT

Pseudocode ends a FOR loop with the line NEXT i. Python has no NEXT line. The indentation does that job. Same words, four spaces moved, different program:

print outside the loop

      
print inside the loop

      

No word changed between those two programs. Four spaces did.

Loops inside loops

The outer loop is the row. The inner loop is the column.

The inner loop runs all the way through on every single pass of the outer one, so a 3 by 4 pair of loops runs the middle twelve times.

a grid

    

For columns that line up, join a tab, "\t", instead of a space. Step 23 uses tabs.

Build the line, then print it. Printing inside the inner loop puts every value on its own line and the grid disappears.

When the inner loop's count depends on the outer counter, for Col in range(Row), you get a staircase instead of a rectangle. That is the whole trick behind every pattern question in steps 24 to 28.

Marks

How the marks are given

Paper 4 awards nothing for describing a loop. It awards marks for a program that runs and prints exactly what was asked for.

The loop is correct and terminates. Right counter, right range, right number of passes.
The body does the job and is indented inside the loop.
The output says what the question asks for. If the question gives the exact words, copy them exactly. This website is stricter than the exam and checks every character, so the habit sticks.
When the question asks for a screenshot of a test run, that mark needs the screenshot in your evidence document. The code marks come from the code you copy in.
Adding up and counting

A total starts at 0. Add to it inside the loop. Print it after.

a running total

    

A count works the same way, but it only adds 1 when an if is true.

a count

    

Print after the loop, not inside it, or you get a line for every pass.

Watch out

Six ways this block goes wrong

  • TRAP“It printed something, so it worked.”

    range(1, 20) gives nineteen lines and every one of them is correct. Nothing is red. Count the lines against what the question asked for.

  • Writing i ^ 2 for squared.

    It does not crash and it does not warn you. It gives a wrong number. Powers are **.

  • Total = i instead of Total = Total + i.

    The answer looks almost plausible, which is what makes it dangerous.

  • Forgetting the colon, or forgetting to indent the next line.

  • Printing inside the inner loop when you wanted a grid.

  • Expecting range(3) to start at 1. It starts at 0, every time.

Scope

What you do and don’t need

You do need

  • a for loop with every form of range, forwards and backwards
  • a running total and a count built inside a loop
  • an if inside a loop, indented correctly
  • a loop inside a loop, and why the inner one finishes first

Not yet, or not at all

  • while loops: that is block D
  • break and continue: block D
  • list comprehensions, enumerate, zip: you will not need them for any mark in this course
  • looping over a list directly; you will use an index instead
Exam thinking

One word picks the loop for you

When a question says how many times, the count is known before the loop starts, so it is a for loop. When it says until, it is not, and you need block D. Reading that one word correctly chooses the right loop before you write a line.

Remember these three

  1. Python's range stops before its second number. FOR i ← 1 TO 10 becomes range(1, 11).
  2. The indentation decides what is inside the loop. There is no NEXT.
  3. The inner loop finishes completely on every pass of the outer loop. Build the line inside, print it outside.
Now practise

Steps 17 to 28

Step 22, Squares, uses the operators from Block A. Read it there.

See all 12 steps

Block C · Loops that count, and loops inside loopsProgress is saved on this device.

Each step is checked against its exact output. Open one to read the task, see a sample run, and write your program. If you get stuck, the task opens the notes that teach it.

  1. 17Count to twentyUse a loop to print the numbers 1 to 20, one per line.Open
  2. 18Count downUse a loop to print 10 down to 1, one per line.Open
  3. 19Times tableAsk which times table. Print it from 1 to 12, one line each, in the form 3 x 7 = 21 (that line is from the 7 times table).Open
  4. 20Add up 1 to 100Use a loop to add up all the numbers from 1 to 100. Print the total.Open
  5. 21How many even numbersUse a loop to count how many even numbers there are from 1 to 50, including 50. Print the count.Open
  6. 22SquaresUse a loop to print the squares of 1 to 12, one per line, in the form 7 squared is 49.Open
  7. 23A times-table gridUse loops to print the 12 by 12 times table as a grid: 12 lines of 12 numbers. Put a tab after each number so the columns line up. In Python a tab is written "\t".Open
  8. 24Square of #Ask for a size. Print a square of # that is that many characters wide and that many lines tall.Open
  9. 25Staircase of #Ask for a size. Print a staircase of #: 1 on the first line, 2 on the next, and so on up to the size.Open
  10. 26Reverse staircaseAsk for a size. Print the staircase upside down: the size on the first line, then one fewer on each line, down to 1.Open
  11. 27PyramidAsk for a size and a symbol. Print a pyramid that many lines tall. Line 1 has 1 symbol, line 2 has 3, line 3 has 5, and so on. Put spaces in front of each line so the pyramid is centred, as in the sample run.Open
  12. 28Fish StretchAsk for a size and a symbol. Print a fish: a diamond for the body, then the tail, which is the pyramid again without its top line. The sample run shows size 4.Open

On a phone? Read the tasks and plan on paper here. The coding is easier on a laptop, where the same link opens the same step.

When you want more than practice

The steps are the front door. The class is where it clicks.

Join a class →Ask on WhatsApp