The answersDownload the PDF
Worksheet

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
NameClassDate

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")
  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 ()
  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
  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
  5. [1 mark]How many spaces is the usual indent for a block inside a block?

  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
  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

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")

Plan your program here, then type it in and press Run.

QR code
Do it on the robot
www.bugbotlab.com/learn/f2-1-blocks-and-indentation/
The simulator checks it and tells you when it passes. Nothing to install, no account.

Challenges

  1. Write a block inside a block inside a block, printing a line at each level.
  2. Indent the first line of a program by one space. What does Python say?
  3. Change the last cell so that "no block" is inside the outer block instead. What changes in the output?