Hard11 marksExtended Response
AQA GCSE · Question 16 · Programming
Question 16 is about a dice game played against a computer. The aim of the game is to get as close to a score of 21 as you can, without going over 21. If your score goes over 21 then you lose. The player's score starts at 0.
For each turn:
- two dice (each numbered from 1 to 6) are rolled
- the total of the two dice rolls is added to the player's score
- the value of each dice and the player's new total is output
- if the current score is less than 21, the player is asked if they would like to roll the dice again: if the player says yes, they get another turn; otherwise, the game ends.
At the end of the game, the program should work as follows:
- if the final score is 21, output a message to say the player has won
- if the final score is greater than 21, output a message to say the player has lost
- if the final score is less than 21, the program generates a random number between 15 and 21 inclusive:
- if this random number is greater than the player's final score, output a message to say the player has lost
- otherwise, output a message to say the player has won.
The dice rolls are carried out by the program generating random numbers between 1 and 6. You will need to use the Python function random.randrange(a, b) which generates a random integer in the range a to b starting at a but finishing one before b.
Write a Python program to simulate this game.
Question 16 is about a dice game played against a computer. The aim of the game is to get as close to a score of 21 as you can, without going over 21. If your score goes over 21 then you lose. The player's score starts at 0.
For each turn:
- two dice (each numbered from 1 to 6) are rolled
- the total of the two dice rolls is added to the player's score
- the value of each dice and the player's new total is output
- if the current score is less than 21, the player is asked if they would like to roll the dice again: if the player says yes, they get another turn; otherwise, the game ends.
At the end of the game, the program should work as follows:
- if the final score is 21, output a message to say the player has won
- if the final score is greater than 21, output a message to say the player has lost
- if the final score is less than 21, the program generates a random number between 15 and 21 inclusive:
- if this random number is greater than the player's final score, output a message to say the player has lost
- otherwise, output a message to say the player has won.
The dice rolls are carried out by the program generating random numbers between 1 and 6. You will need to use the Python function random.randrange(a, b) which generates a random integer in the range a to b starting at a but finishing one before b.
Write a Python program to simulate this game.
How to approach this question
1. **Import:** Start with `import random`.
2. **Initialise Variables:** Set up `player_score = 0` and a loop control variable like `roll_again = "yes"`.
3. **Main Game Loop:** Create a `while` loop that continues as long as `roll_again` is "yes".
4. **Roll Dice:** Inside the loop, generate two random numbers for the dice. `random.randrange(1, 7)` will give numbers from 1 to 6.
5. **Update and Display Score:** Add the dice rolls to `player_score` and print the individual rolls and the new total.
6. **Check Game State:**
* If `player_score` is 21 or more, the game must end. Set `roll_again = "no"` to stop the loop.
* If `player_score` is less than 21, ask the user if they want to roll again and update the `roll_again` variable with their answer.
7. **Endgame Logic:** After the `while` loop finishes, use a multi-way `if/elif/else` structure to determine the final outcome based on the rules.
* `if player_score == 21:` -> Win.
* `elif player_score > 21:` -> Lose.
* `else:` (meaning score is < 21):
* Generate the computer's score. `random.randrange(15, 22)` will give numbers from 15 to 21.
* Use a nested `if/else` to compare the player's score to the computer's score and print the final win/loss message.
Full Answer
python
import random
player_score = 0
roll_again = "yes"
while roll_again.lower() == "yes":
die1 = random.randrange(1, 7) # Generates 1-6
die2 = random.randrange(1, 7) # Generates 1-6
print(f"Roll 1: {die1}")
print(f"Roll 2: {die2}")
player_score += die1 + die2
print(f"Current score: {player_score}")
if player_score >= 21:
roll_again = "no" # End the game automatically
else:
roll_again = input("Would you like to roll again? ")
# Game has ended, now determine the outcome
if player_score == 21:
print("You won!")
elif player_score > 21:
print("You lost!")
else: # player_score is < 21
computer_score = random.randrange(15, 22) # Generates 15-21
print(f"Your final score is {player_score}")
print(f"Computer's score is {computer_score}")
if computer_score > player_score:
print("You lost!")
else:
print("You won!")
This is a complex programming task that combines loops, conditional logic, and random number generation.
**Program Structure:**
1. **Setup:** The program begins by importing the `random` library and setting initial values for the `player_score` and a variable to control the main loop, `roll_again`.
2. **Game Loop (`while`):** A `while` loop is used to manage the player's turns. It continues as long as the player wants to roll again.
- **Dice Rolling:** `random.randrange(1, 7)` is used to simulate a 6-sided die. Note that `randrange(a, b)` generates numbers from `a` up to, but not including, `b`. So `randrange(1, 7)` produces integers from 1 to 6.
- **State Update:** The score is updated, and the results are printed.
- **Loop Control:** The crucial part is deciding whether to continue. If the score hits or exceeds 21, the loop must be forced to stop. Otherwise, the player's input determines the next iteration. Using `.lower()` on the input makes the check case-insensitive (accepting "yes", "Yes", "YES", etc.).
3. **Outcome Logic (`if/elif/else`):** Once the loop terminates, a sequence of `if/elif/else` statements checks the `player_score` against the game's rules to print the final result. This includes the special case where the player "sticks" on a score less than 21, triggering a comparison with a randomly generated computer score.
Common mistakes
✗ Incorrect range for `random.randrange`, e.g., `(1, 6)` would only generate 1-5.\n✗ Loop condition that doesn't correctly terminate the game (e.g., allowing the player to roll again after busting).\n✗ Messy endgame logic; a clean `if/elif/else` structure is best.\n✗ Not handling the computer's score generation and comparison correctly.
Practice the full AQA GCSE Computer Science Paper 1 Python
31 questions · hints · full answers · grading
More questions from this exam
Q01.1Figure 1 shows an algorithm, represented using pseudo-code, which assigns a different value to fo...EasyQ01.2The variable `x` is assigned a value using the statement:
`x ← LEN(state)`
Using Figure 1, what ...EasyQ01.3What is the result of concatenating the contents of the variables `city` and `landmark` in Figure 1?EasyQ01.5The subroutine `POSITION` finds the first position of a character in a string.
For example, `POSI...EasyQ02.1Figure 2 shows an algorithm that uses integer division which has been represented using pseudo-co...Easy
Expert