Functions and structured code · GCSE · OCR J277 2.2.3, AQA 8525 3.2.10, Edexcel 1CP2 6.6.1 · about 15 min
import, Python's own libraries, and a module file of your own.
[1 mark]What does this program print?
import math print(math.sqrt(49))
7.0
sqrt from the math library gives a real number, so 7.0.
[1 mark]What does this program print?
from math import floor, ceil print(floor(2.7), ceil(2.1))
2 3
from ... import brings in just those names; floor rounds down and ceil rounds up.
[1 mark]Which are benefits of using a library?
Tick every answer that is true.
[1 mark]A program says import moves and calls moves.square(20). Where is square defined?
[1 mark]Why does moves.py need its own from bugbot import * line?
Finish square(size) in moves.py above so it drives a square and ends where it started (replace pass). Then, in the task, import moves and use it to drive a square of 20 cm and then a square of 35 cm. Do not write the square's drives in the task program itself.
# the two lines every program starts with: the commands, then the robot from bugbot import * connect() import moves moves.square(20)
The hint students can ask for: Put the driving function in the other file, with the size as its parameter. The task program imports that file and calls the function twice with different sizes.
{'program': 'from bugbot import *\nconnect()\nimport moves\n\nmoves.square(20)\nmoves.square(35)\n', 'files': {'moves.py': '# moves.py: driving moves, for any program that imports it\nfrom bugbot import *\n\ndef zigzag(length):\n """Drive a zigzag: forward and right, then forward and left."""\n forward(60, distance=length)\n right(60, distance=length)\n forward(60, distance=length)\n left(60, distance=length)\n\ndef square(size):\n """Drive a square with sides of size cm, ending where it started."""\n forward(60, distance=size)\n right(60, distance=size)\n backward(60, distance=size)\n left(60, distance=size)\n'}}Any program that meets the task's checks is marked correct in the simulator; this is one way, not the only way.