The worksheetDownload the PDF
Answers

F1.6 Variables, constants and assignment

Programming basics · GCSE · OCR J277 2.2.1, AQA 8525 3.2.2, Edexcel 1CP2 6.3.2 · about 15 min

BugBotLab

What this lesson is about

Storing, changing and naming values, and constants that never change.

Questions 8 marks in all

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

    count = 0
    count = count + 1
    count = count + 1
    print(count)
    Answer:
    2

    Each count = count + 1 works out the right-hand side with the current value, then stores the result back.

  2. [1 mark]What does the = in speed = 60 mean?

    1. AStore the value 60 under the name speed
    2. Bspeed is equal to 60, as in maths
    3. CCheck whether speed is 60
    4. DPrint speed
    Answer: A. = stores a value in a name. Read it right to left: take 60 and store it in speed.
  3. [1 mark]Which of these are allowed as variable names?

    Tick every answer that is true.

    1. Aleg_length
    2. Bspeed2
    3. C2speed
    4. Dleg length
    Answer: A, B. Names use letters, numbers and underscores, cannot start with a number, and cannot contain spaces.
  4. [1 mark]speed = 60 then print(Speed). What happens?

    1. ANameError: Speed is a different name from speed
    2. BIt prints 60
    3. CIt prints Speed
    4. DIt prints 0
    Answer: A. Names are case-sensitive, and Speed was never given a value.
  5. [1 mark]What does this program print?

    leg = 24
    half = leg / 2
    print(leg, half)
    Answer:
    24 12.0

    half stores the result of the calculation, and dividing gives a float.

  6. [1 mark]Why store before = distance() before driving?

    1. ASo the first reading can be compared with one taken later
    2. BBecause distance() only works once
    3. CTo make the robot drive further
    4. DIt makes no difference
    Answer: A. Without a variable, the first reading would be gone by the time you took the second.
  7. [1 mark]What is the difference between a variable and a constant?

    1. AA variable's value can change while the program runs; a constant's does not
    2. BA constant can hold text and a variable cannot
    3. CA variable is faster
    4. DThere is no difference in Python
    Answer: A. A constant is set once and not changed. Python does not enforce it, so constants are written in capitals as a promise.
  8. [1 mark]Four drives all use MAX_SPEED. Why is that better than typing 40 four times?

    1. AThe speed only has to be changed in one place
    2. BIt makes the robot drive further
    3. CPython runs constants faster
    4. DNumbers are not allowed as arguments
    Answer: A. One place to change the value, and no chance of missing one of the four copies.

The task: speed report

Store 70 in a variable called speed. Print speed: 70 and half: 35, both using the variable (the number 70 appears only on the first line, and 35 nowhere). Dividing with / prints 35.0, which is not 35: // divides and keeps a whole number, and lesson F1.9 explains the difference. Then drive 20 cm at that speed.

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

speed = 70
print("speed:", speed)

The hint students can ask for: Put 70 in a variable called speed. Print speed: 70 and half: 35 using the variable, then drive 20 cm at that speed.

A solution

from bugbot import *
connect()
speed = 70
print('speed:', speed)
print('half:', speed // 2)
forward(speed, distance=20)

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