The worksheetDownload the PDF
Answers

F2.1 Blocks and indentation

Decisions and loops · GCSE · OCR J277 2.2.1, AQA 8525 3.2.2, Edexcel 1CP2 6.2.1 · about 10 min

BugBotLab

What this lesson is about

How Python groups lines: the colon and the indent.

Questions 7 marks in all

  1. [1 mark]What does this program print?

    if True:
        print("one")
        print("two")
    print("three")
    Answer:
    one
    two
    three

    The two indented lines are the block. The unindented line is not part of it and runs anyway.

  2. [1 mark]What must a line that starts a block end with?

    1. AA colon :
    2. BA semicolon ;
    3. CA full stop
    4. DBrackets ()
    Answer: A. The colon says a block follows, and the next lines must be indented.
  3. [1 mark]What does Python say about a line indented for no reason, with no : line above it?

    1. AIndentationError: unexpected indent
    2. BNothing, it runs normally
    3. CNameError
    4. DIt skips the line
    Answer: A. Indentation must belong to a block. Indentation with no block is an error.
  4. [1 mark]Two lines in the same block are indented by four spaces and three spaces. What happens?

    1. AIndentationError: lines in one block must line up exactly
    2. BBoth run
    3. COnly the first runs
    4. DPython fixes it
    Answer: A. What Python cares about is that every line in the same block has the same indentation.
  5. [1 mark]How many spaces is the usual indent for a block inside a block?

    Answer: 8. Four spaces for each level: a block inside a block is eight spaces in.
  6. [1 mark]Where does a block end?

    1. AAt the first line that is indented less
    2. BAt the next blank line
    3. CAt the next print
    4. DAt the end of the program, always
    Answer: A. The first line indented less than the block is outside it.
  7. [1 mark]In OCR's Exam Reference Language, which word closes an if block?

    1. Aendif
    2. Bend
    3. Cfi
    4. DNothing: indentation is enough
    Answer: A. The Reference Language closes blocks with words: endif, endwhile, next. Python uses indentation alone.

The task: fix the indentation

This program's lines are in the right order but the indentation is wrong. It should print inside the block, then leave the LED green, then print after the block once. Fix only the spaces.

# the two lines every program starts with: the commands, then the robot
from bugbot import *
connect()

if True:
print("inside the block")
# the LED: green
led("green")
    print("after the block")

The hint students can ask for: Fix the indentation only. The two led and print lines belong inside the if; 'after the block' belongs outside it.

A solution

from bugbot import *
connect()
if True:
    print('inside the block')
    led('green')
print('after the block')

Any program that meets the task's checks is marked correct in the simulator; this is one way, not the only way.