WEB PRACTICE CHAPTER 09

Games and Simulations

Ten additional coding tasks · W081–W090

These extra website exercises are not a substitute for the book's exact numbered questions. Follow your printed book separately using the 100-challenge tracker.
Chapter 9 code

Choose starters to try yourself, or download a separate worked example pack after attempting the tasks.

Chapter 9 starters ↓Chapter 9 worked examples ↓
WEB PRACTICE W081

Roll two dice

Use Python random to simulate two dice and display their total.

Reveal a hint

random.Random(42) gives reproducible results for testing.

Reveal example code (after trying it yourself)
import random
rng = random.Random(42)
dice = [rng.randint(1, 6) for _ in range(2)]
print('Dice:', dice, 'Total:', sum(dice))

One possible solution. Try writing your own first.

Extension: Change the sample values and test at least two different cases.

WEB PRACTICE W082

Coin tosses

Generate ten reproducible coin tosses and count the heads.

Reveal a hint

Use rng.choice() and list.count().

Reveal example code (after trying it yourself)
import random
rng = random.Random(7)
tosses = [rng.choice(['H','T']) for _ in range(10)]
print(tosses, 'Heads:', tosses.count('H'))

One possible solution. Try writing your own first.

Extension: Change the sample values and test at least two different cases.

WEB PRACTICE W083

Random study topic

Choose one study topic from a fixed list.

Reveal a hint

Use random.choice with a local generator.

Reveal example code (after trying it yourself)
import random
topics = ['loops', 'lists', 'files', 'functions']
print(random.Random(3).choice(topics))

One possible solution. Try writing your own first.

Extension: Change the sample values and test at least two different cases.

WEB PRACTICE W084

Number guessing practice

Generate a secret and evaluate a sample guess.

Reveal a hint

Check equality before checking high or low.

Reveal example code (after trying it yourself)
import random
secret = random.Random(11).randint(1, 10)
guess = 5
if guess == secret:
    print('Correct')
elif guess > secret:
    print('Too high')
else:
    print('Too low')
print('Secret was:', secret)

One possible solution. Try writing your own first.

Extension: Change the sample values and test at least two different cases.

WEB PRACTICE W085

Three-round score

Simulate three game rounds and total the awarded points.

Reveal a hint

Maintain a running score.

Reveal example code (after trying it yourself)
import random
rng = random.Random(22)
total = 0
for round_number in range(1, 4):
    points = rng.randint(1, 10)
    total += points
    print('Round', round_number, 'points:', points)
print('Final score:', total)

One possible solution. Try writing your own first.

Extension: Change the sample values and test at least two different cases.

WEB PRACTICE W086

Shuffle a quiz

Shuffle a list of five quiz questions reproducibly.

Reveal a hint

random.shuffle modifies a list in place.

Reveal example code (after trying it yourself)
import random
questions = ['Q1','Q2','Q3','Q4','Q5']
random.Random(13).shuffle(questions)
print('Quiz order:', questions)

One possible solution. Try writing your own first.

Extension: Change the sample values and test at least two different cases.

WEB PRACTICE W087

Simulate a spinner

Simulate 20 spins of a four-colour spinner and count outcomes.

Reveal a hint

Count each colour with a dictionary.

Reveal example code (after trying it yourself)
import random
rng = random.Random(8)
colours = ['red','blue','green','yellow']
counts = {colour:0 for colour in colours}
for _ in range(20):
    counts[rng.choice(colours)] += 1
print(counts)

One possible solution. Try writing your own first.

Extension: Change the sample values and test at least two different cases.

WEB PRACTICE W088

Grid movement

Apply four moves to a grid character starting at (0,0).

Reveal a hint

Update x or y for each move.

Reveal example code (after trying it yourself)
moves = ['right','up','up','left']
x = y = 0
for move in moves:
    if move == 'right': x += 1
    elif move == 'left': x -= 1
    elif move == 'up': y += 1
    elif move == 'down': y -= 1
print('Final position:', (x,y))

One possible solution. Try writing your own first.

Extension: Change the sample values and test at least two different cases.

WEB PRACTICE W089

Rock-paper-scissors

Evaluate a sample player choice against a reproducible computer choice.

Reveal a hint

Wins are cyclic: rock beats scissors, etc.

Reveal example code (after trying it yourself)
import random
options = ['rock','paper','scissors']
computer = random.Random(17).choice(options)
player = 'paper'
wins = {('rock','scissors'),('paper','rock'),('scissors','paper')}
outcome = 'Draw' if player == computer else 'Win' if (player,computer) in wins else 'Lose'
print('Computer:', computer, 'Player:', player, outcome)

One possible solution. Try writing your own first.

Extension: Change the sample values and test at least two different cases.

WEB PRACTICE W090

Movement simulation

Simulate five steps forwards with random left/right turns.

Reveal a hint

A seed allows you to reproduce the same path.

Reveal example code (after trying it yourself)
import random
rng = random.Random(21)
position = 0
for _ in range(5):
    position += rng.choice([-1,1])
    print('Position:', position)

One possible solution. Try writing your own first.

Extension: Change the sample values and test at least two different cases.