The answersDownload the PDF
Worksheet

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
NameClassDate

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
  2. [1 mark]What does this program print?

    print("before")
    # print("middle")
    print("after")
  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
  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
  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
  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

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

# ...

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

QR code
Do it on the robot
www.bugbotlab.com/learn/f1-5-comments-and-readable-code/
The simulator checks it and tells you when it passes. Nothing to install, no account.

Challenges

  1. Take any program from an earlier lesson and add comments that explain why, not what.
  2. Comment out the stop() in a driving program. What stops the robot in the end?
  3. Write a program that is only comments. Does it run?