The worksheetDownload the PDF
Answers

F1.8 Data types and casting

Programming basics · GCSE · OCR J277 2.2.2, AQA 8525 3.2.1, Edexcel 1CP2 6.3.1 · about 15 min

BugBotLab

What this lesson is about

Integer, real, Boolean, character and string, and converting between them.

Questions 7 marks in all

  1. [1 mark]Which data type best suits whether the robot has bumped into something?

    1. ABoolean
    2. BInteger
    3. CString
    4. DReal
    Answer: A. It is either true or false, so a Boolean.
  2. [1 mark]Which data type best suits a distance measured to a tenth of a centimetre, such as 41.3?

    1. AReal
    2. BInteger
    3. CBoolean
    4. DCharacter
    Answer: A. It has a fractional part, so a real number (a float in Python).
  3. [1 mark]What does this program print?

    print(int("20") * 3)
    print("20" * 3)
    Answer:
    60
    202020

    int turns the text into a number, so the first line multiplies. The second repeats the text.

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

    print(int(7.9))
    Answer:
    7

    int chops off the fractional part. It does not round.

  5. [1 mark]What happens with int("twenty")?

    1. AA ValueError: there is no number in the text
    2. BIt gives 20
    3. CIt gives 0
    4. DIt gives the string "twenty"
    Answer: A. Casting only works when the text holds a number in the right form. This is a runtime error.
  6. [1 mark]What is casting?

    1. AConverting a value from one data type to another
    2. BStoring a value in a variable
    3. CPrinting a value
    4. DRounding a number
    Answer: A. int(), float() and str() are casting functions.
  7. [1 mark]Which function turns "2.5" into the number 2.5?

    Answer: float. float converts to a real number. int would refuse text with a decimal point.

The task: drive what you type

Ask How far? , turn the answer into a number, and drive exactly that far forward. The task will answer 35, so the robot should stop 35 cm up the mat. Your program must work for any distance: do not type 35 into it.

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

far = input("How far? ")
forward(50, distance=far)

The hint students can ask for: What input() gives you is always text, so turn the answer into a number before you drive with it.

A solution

from bugbot import *
connect()
far = float(input("How far? "))
forward(50, distance=far)

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