AQA GCSE Computer Science June 2023 Paper 1, Question 16: the dice game to 21
AQA 8525 June 2023 Paper 1, Question 16: the 11 mark program. Write a Python dice game where you roll two dice towards 21 and play against the computer. Built in four small steps, the eleven marks explained, and the game to play.
Question 16 is the last question of the AQA GCSE Computer Science Paper 1 sat on 19 May 2023 (8525/1B, the Python paper), and the biggest: 11 marks for one program. It fills a page and a half of the paper. Do not let the length put you off. Four of the marks are for design, and you can collect most of the others a piece at a time.
We do not copy the exam paper here. Open it beside this page: AQA June 2023 Paper 1B question paper (PDF). When you have finished, check the mark scheme too.
The question in short
The player tries to get as close to 21 as they can without going over. The score starts at 0.
Each turn:
- two dice are rolled, each 1 to 6;
- both rolls are added to the score;
- each roll and the new score are output;
- if the score is less than 21, the player is asked whether to roll again. Yes means another turn. Anything else ends the game.
At the end:
- a score of exactly 21 wins;
- a score over 21 loses;
- a score under 21 plays the computer: the program picks a random number from 15 to 21. If that number is greater than the player's score the player loses. Otherwise the player wins.
The paper reminds you that random.randrange(a, b) starts at a and stops one before b, and gives you import random as the first line.
Build it in four steps
A long question is several short ones. Write each piece, then move on.
Step 1: one turn. Roll, add, output.
roll1 = random.randrange(1, 7)
roll2 = random.randrange(1, 7)
score = score + roll1 + roll2
print("Roll 1:", roll1)
print("Roll 2:", roll2)
print("Current score:", score)
Step 2: repeat the turn. The player chooses whether to go on, but only if the score is under 21. One variable, again, controls the loop. When the score reaches 21 or more the program sets it to "no" itself.
Step 3: the easy endings. Exactly 21 wins. Over 21 loses.
Step 4: play the computer. Only needed when the score is under 21, so it goes in the final else.
A model answer
import random
score = 0
again = "yes"
while again == "yes":
roll1 = random.randrange(1, 7)
roll2 = random.randrange(1, 7)
score = score + roll1 + roll2
print("Roll 1:", roll1)
print("Roll 2:", roll2)
print("Current score:", score)
if score < 21:
again = input("Would you like to roll again? ")
else:
again = "no"
if score == 21:
print("You won!")
elif score > 21:
print("You lost!")
else:
computer = random.randrange(15, 22)
if computer > score:
print("You lost!")
else:
print("You won!")
Where the eleven marks are
Four marks are for design. You get them for the attempt, even if the code is not quite right:
- trying to generate two random numbers;
- using selection to compare the score with 21;
- using a loop to keep rolling;
- outputting the dice rolls in a sensible place.
Seven marks are for a program that works:
- two random numbers from 1 to 6 inclusive;
- adding both to a running total;
- a loop that ends when the score is under 21 and the player says no;
- the game ending when the score reaches 21 or more;
- selection that correctly spots a loss (over 21) or a win (exactly 21);
- a random number from 15 to 21 inclusive, made in the right place, and compared with the score;
- at least one correct win or lose message in the right place.
Any error caps you at 10. If you make the same off-by-one slip in both randrange calls, you are only penalised once.
Where the marks are lost
random.randrange(1, 6). It stops one before 6, so the dice can never roll a six. It must be(1, 7), and the computer's number must be(15, 22). The paper tells you this in so many words.random.randint(1, 6)is accepted too, and it includes both ends.- Rolling once, before the loop. Then every turn adds the same two numbers. The rolls belong inside the loop.
score = roll1 + roll2. That replaces the score. It must be added to the old score.- Asking "roll again?" when the score is 21 or more. The question says to ask only when the score is less than 21.
- Making the computer's number at the top of the program. It is only needed in the last case, and that is where the mark scheme wants it.
- Leaving it blank. A loop, two
randrangecalls, anifon 21 and aprintof the rolls are four design marks, even if nothing else works.
Run it
Play it. The robot's light goes green if you win and red if you lose. Type yes to roll again.
The program
from bugbot import *
import random
connect()
score = 0
again = "yes"
while again == "yes":
roll1 = random.randrange(1, 7)
roll2 = random.randrange(1, 7)
score = score + roll1 + roll2
print("Roll 1:", roll1)
print("Roll 2:", roll2)
print("Current score:", score)
if score < 21:
again = input("Would you like to roll again? ")
else:
again = "no"
if score == 21:
won = True
elif score > 21:
won = False
else:
computer = random.randrange(15, 22)
print("The computer has", computer)
won = computer <= score
if won:
led("green")
print("You won!")
else:
led("red")
print("You lost!")
Now change it
What score should you stop at? Change the program so that it plays by itself: it rolls again whenever the score is below a number called STICK, with no input. Wrap the game in a loop that plays 1000 games and counts the wins. Try STICK at 14, 16 and 18. Which wins most often?
Questions
What is the answer to AQA GCSE Computer Science 2023 Paper 1 Question 16?
Use a while loop that rolls two dice with random.randrange(1, 7), adds them to the score and prints them, and asks whether to roll again only while the score is under 21. After the loop, 21 wins and over 21 loses. Under 21, generate random.randrange(15, 22): the player loses if it is greater than the score and wins otherwise.
How do I get a random number from 1 to 6 in Python?
Use random.randrange(1, 7) or random.randint(1, 6), after import random. randrange stops one before its second number. randint includes it.
How do I get marks on a long programming question if I cannot finish it?
Write the parts you can. On AQA papers several marks are for design: using a loop, using selection, generating the random numbers, outputting values. They are given even when the syntax is wrong or the program does not fully work.
More from this paper
- Question 7: A ticket price with a group discount 6 marks
- Question 12.2: Write your own linear search 7 marks
- Question 13: Validate a grid reference until it is correct 6 marks
- Question 15: Share a bill: loop until it is paid, then report the tip 8 marks
Every AQA 8525 question we have worked
Learn it step by step
- F3.7 Random numbers Strings, lists and records
- F2.6 Condition-controlled loops: while Decisions and loops
- F13.4 Programming questions Exam preparation
This is our own explanation of a published exam question. It is not written or endorsed by AQA, and the question paper and mark scheme remain AQA's copyright. Read them on AQA's site with the links on this page.