Edexcel GCSE Computer Science June 2022 Paper 2, Question 2: fixing the turtle program

Pearson Edexcel 1CP2/02 June 2022, Question 2: fix a NameError, an AttributeError, a TypeError and three logic errors in a turtle program that draws axes, a square and a gold circle, then add a pen size, a pen colour and a hidden turtle. Each fix explained, with the finished program and a robot that drives the shape.

Past paper questionPearson 1CP2/02June 2022 Paper 210 marksFix the code

Question 2 of the Pearson Edexcel GCSE Computer Science Paper 2 from the June 2022 series (1CP2/02, Application of Computational Thinking) is 10 marks of turtle graphics debugging. The paper names each line and each kind of error. Turtle is only a library: every fix is ordinary Python.

We do not copy the exam paper or Pearson's code files here. Open the paper beside this page: Edexcel June 2022 Paper 2 question paper (PDF). When you have finished, check the mark scheme too.

The question in short

The finished picture is a 400 by 400 square with a circle 200 across inside it, both centred on a pair of axes. The circle outline is gold, everything else black. The given program has errors and gaps.

The ten fixes

The paper says The fix Why
add a comment for the data type of the argument in turtle.mode("standard") # "standard" is a string any comment with the word string near that line
NameError: screen.setup(WIDTH, HIGHT) HEIGHT the constant is spelt HEIGHT where it is defined
AttributeError: turtle.turtle() turtle.Turtle() the class has a capital T; the library has none
TypeError: theTurtle.pendown(200) theTurtle.pendown() pendown takes no argument
logic error: the vertical axis is too far right, setpos(100, 200) setpos(0, 200) the axis runs down the middle, x = 0
logic error: the vertical axis is too short, forward(100) forward(400) it has to span the square, 400
logic error: the square tilts left, setheading(95) setheading(90) 90 is straight up
set the pen size to the constant BIG theTurtle.pensize(BIG) a constant, not a number
set the pen colour to gold theTurtle.pencolor("gold") before the circle is drawn
hide the turtle at the end theTurtle.hideturtle()

One mark each. The three added lines must use theTurtle, the turtle the program made, not the library's default turtle.

What the error names tell you

  • NameError: a name Python has never seen. Look for a misspelling of something defined nearby.
  • AttributeError: the object exists but has no such thing inside it. Here the library turtle has Turtle (a class) but not turtle (nothing).
  • TypeError: the right function, the wrong kind or number of arguments.
  • Logic error: no message. Run it and look at the picture.

Where the marks are lost

  • turtle.pensize(BIG) on the default turtle instead of theTurtle. The first such line is marked wrong.
  • Fixing the tilt to 0 or 180. The square starts by heading up. Straight up is 90.
  • Deleting the pendown line instead of fixing it. Then nothing is drawn.
  • Changing lines you were not asked to change. The paper says not to.

Run it

Turtle graphics does not run in this simulator, so here is the finished picture as a plan and a robot that drives it: up the vertical axis, then round the square. Below it is the turtle program you would save in the exam.

The robot draws the vertical axis and then the square in the same order as the turtle program, scaled to 40 cm.
The program
from bugbot import *
connect()

SCALE = 0.1          # 400 turtle units become 40 cm
BIG = 3

# the vertical axis: from the bottom of the square to the top
forward(60, distance=400 * SCALE)
backward(60, distance=400 * SCALE)

# the square, starting at its bottom left corner, heading up
left(60, distance=200 * SCALE)
led("green")
for side in range(4):
    forward(60, distance=400 * SCALE)
    turn_right(30, angle=90)
print("axis and square drawn")
Put this demo on your own site

Paste it into a school website, Moodle, Google Sites or a blog. More options on the embed page.

import turtle

WIDTH = 600
HEIGHT = 600
BIG = 3

turtle.mode("standard")       # "standard" is a string
screen = turtle.Screen()
screen.setup(WIDTH, HEIGHT)
theTurtle = turtle.Turtle()

# the axes
theTurtle.penup()
theTurtle.setpos(-200, 0)
theTurtle.pendown()
theTurtle.forward(400)
theTurtle.penup()
theTurtle.setpos(0, 200)
theTurtle.setheading(270)
theTurtle.pendown()
theTurtle.forward(400)

# the square
theTurtle.penup()
theTurtle.setpos(-200, -200)
theTurtle.setheading(90)
theTurtle.pendown()
for side in range(4):
    theTurtle.forward(400)
    theTurtle.right(90)

# the circle
theTurtle.penup()
theTurtle.setpos(0, -100)
theTurtle.setheading(0)
theTurtle.pensize(BIG)
theTurtle.pencolor("gold")
theTurtle.pendown()
theTurtle.circle(100)
theTurtle.hideturtle()
turtle.done()

Questions

What is an AttributeError in Python?

An error raised when you ask an object for something it does not have, such as a method spelt with the wrong capital letter. turtle.turtle() fails because the library's class is called Turtle.

How do I set the pen colour and size in turtle graphics?

Call pencolor("gold") and pensize(3) on your turtle object before drawing. They apply to everything drawn after them.

What heading is straight up in turtle graphics?

90 degrees. 0 is to the right, 180 is to the left and 270 is straight down.

More from this paper

Every Edexcel 1CP2 question we have worked

Learn it step by step

  1. F1.4 Errors: syntax, runtime and logic Programming basics
  2. F6.4 Debugging logic errors Robust programs
Open the lessons

This is our own explanation of a published exam question. It is not written or endorsed by Pearson, and the question paper and mark scheme remain Pearson's copyright. Read them on Pearson's site with the links on this page.