The answersDownload the PDF
Worksheet

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
NameClassDate

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

    leg = 24
    half = leg / 2
    print(leg, half)
  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
  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
  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

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)

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

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

Challenges

  1. Store the battery in a variable at the start, drive around a bit, and print how much battery the drive used.
  2. Add a constant NOTE = 440 to a program and use it for three short beeps with tone.
  3. What happens if you use a variable before giving it a value? Try it and read the error. Which of the three kinds of error is it?