Edexcel GCSE Computer Science sample Paper 2, Question 1: rolling a dice

Pearson Edexcel 1CP2/02 sample assessment material, Question 1: complete a Python program that imports random, declares a variable and a constant, rolls a dice with randint and displays the result. The seven marks explained, with the program to run.

Past paper questionPearson 1CP2/02Sample Paper 27 marksComplete the program

Question 1 of Pearson's sample assessment material for Edexcel GCSE Computer Science Paper 2 (1CP2/02, Application of Computational Thinking) is the on-screen paper's opener: 7 marks for five short lines added to a given file.

We do not copy the paper or Pearson's code files here. Open the paper beside this page: Edexcel 1CP2/02 sample assessment materials (PDF). The mark scheme is in the same document.

The question in short

The program simulates a dice roll with a random integer from 1 to 6. You add lines to: import the random library; create one variable; create one constant; assign the result of a library call to the variable; display a message and the variable.

A finished program

import random

SIDES = 6
roll = 0

roll = random.randint(1, SIDES)
print("You rolled", roll)

Where the seven marks are

import random; roll created and set to an integer (two marks: the name and the value); SIDES created and set to 6 (two marks); roll = random.randint(1, SIDES); and a print with a message and the variable (two marks). The message and the number may be printed on separate lines.

Where the marks are lost

  • sides = 6 in lower case. Edexcel's convention for a constant is capitals, and the marks distinguish the constant from the variable.
  • random.randint(0, 6). A dice has no 0. And randrange(1, 6) can never roll a 6.
  • randint(1, 6) without random. The function belongs to the library.
  • Typing 6 into the call when the constant is there. The point of the constant is to use it.

Run it

The robot rolls: it spins a random number of times and shows the result.

A different roll each run, always from 1 to 6. The robot turns once for each spot.
The program
from bugbot import *
import random
connect()

SIDES = 6
roll = 0

roll = random.randint(1, SIDES)
print("You rolled", roll)
for spin in range(roll):
    turn_right(60, angle=60)
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.

Questions

What is the answer to Edexcel sample Paper 2 Question 1?

import random, then SIDES = 6, roll = 0, roll = random.randint(1, SIDES), and print("You rolled", roll).

What is the difference between a variable and a constant?

A variable's value can change while the program runs. A constant's value is set once and never changed, and by convention its name is in capitals.

How do I get a random whole number from 1 to 6 in Python?

random.randint(1, 6) after import random. Both ends are included.

More from this paper

Every Edexcel 1CP2 question we have worked

Learn it step by step

  1. F3.7 Random numbers Strings, lists and records
  2. F1.6 Variables, constants and assignment Programming basics
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.