Programming basics · GCSE · OCR J277 2.2.2, AQA 8525 3.2.1, Edexcel 1CP2 6.3.1 · about 15 min
Integer, real, Boolean, character and string, and converting between them.
[1 mark]Which data type best suits whether the robot has bumped into something?
[1 mark]Which data type best suits a distance measured to a tenth of a centimetre, such as 41.3?
[1 mark]What does this program print?
print(int("20") * 3)
print("20" * 3)60 202020
int turns the text into a number, so the first line multiplies. The second repeats the text.
[1 mark]What does this program print?
print(int(7.9))
7
int chops off the fractional part. It does not round.
[1 mark]What happens with int("twenty")?
[1 mark]What is casting?
[1 mark]Which function turns "2.5" into the number 2.5?
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.
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.