The worksheetDownload the PDF
Answers

F1.5 Comments and readable code

Programming basics · GCSE · OCR J277 2.3.1, AQA 8525 3.2.2, Edexcel 1CP2 6.1.4 · about 10 min

BugBotLab

What this lesson is about

Comments, names, blank lines: code a person can read and change.

Questions 6 marks in all

  1. [1 mark]What does Python do with a line that starts with #?

    1. ASkips it completely
    2. BPrints it
    3. CRuns it twice
    4. DTreats it as an error
    Answer: A. A comment is there for people. Python ignores it.
  2. [1 mark]What does this program print?

    print("before")
    # print("middle")
    print("after")
    Answer:
    before
    after

    Putting # in front of a line switches it off, so the middle line does not run.

  3. [1 mark]Which comment is the most useful?

    1. Aled("red") # warn: battery is low
    2. Bled("red") # set the LED to red
    3. Cled("red") # led
    4. Dled("red") # a line of code
    Answer: A. A good comment says why. Saying what the line obviously does adds nothing.
  4. [1 mark]What do blank lines between parts of a program change about how it runs?

    1. ANothing: they only make it easier to read
    2. BThey make it run slower
    3. CThey cause an error
    4. DThey end the program
    Answer: A. Python ignores blank lines. Like paragraphs, they separate the steps for the reader.
  5. [1 mark]Why do programmers comment a line out instead of deleting it?

    1. ATo switch it off while testing, and put it back easily
    2. BTo make the program faster
    3. CBecause deleting is not allowed
    4. DTo make Python print it
    Answer: A. Removing the # brings the line straight back.
  6. [1 mark]Which of these make a program easier to maintain?

    Tick every answer that is true.

    1. AComments that say why
    2. BMeaningful variable names
    3. CBlank lines between steps
    4. DPutting the whole program on as few lines as possible
    Answer: A, B, C. Comments, meaningful names, indentation and white space all help the next person read and change the code. Squashing it up does the opposite.

The task: a tidy drive

Write a program that turns the LED on, drives forward for a second, stops, turns the LED off and prints done. Put a comment above each part saying what it is for, and a blank line between the parts.

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

# ...

The hint students can ask for: LED on, drive forward for a second, stop, LED off, print 'done'. Put a comment above each part saying what it does.

A solution

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

# Light on: the program has started
led("green")

# Drive forward for a second
forward(50)
wait(1)

# Stop before the edge of the mat
stop()

# Light off and report: the program has finished
led("off")
print("done")

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