Decisions and loops · GCSE · OCR J277 2.2.1, AQA 8525 3.2.2, Edexcel 1CP2 6.2.1 · about 10 min
How Python groups lines: the colon and the indent.
[1 mark]What does this program print?
if True:
print("one")
print("two")
print("three")one two three
The two indented lines are the block. The unindented line is not part of it and runs anyway.
[1 mark]What must a line that starts a block end with?
:;()[1 mark]What does Python say about a line indented for no reason, with no : line above it?
[1 mark]Two lines in the same block are indented by four spaces and three spaces. What happens?
[1 mark]How many spaces is the usual indent for a block inside a block?
[1 mark]Where does a block end?
[1 mark]In OCR's Exam Reference Language, which word closes an if block?
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.
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.