1 The Problem
We want a program that rolls a dice — gives a random number from 1 to 6 — and can roll several at once if asked. It teaches two ideas every game and simulation needs: randomness and doing something N times.
2 How to Think About It
Picture what the program does, step by step, before any code:
3 The Build — explained part by part
Here is the complete dice roller. Each line is explained below.
import random
# Ask how many dice to roll, and turn the text into a number.
count = int(input("How many dice? "))
total = 0
# Do the following 'count' times.
for i in range(count):
roll = random.randint(1, 6) # a random whole number from 1 to 6
print(f"Dice {i + 1}: {roll}")
total = total + roll # add this roll to the running total
print(f"Total: {total}")
count = int(input(...)) — ask how many dice and turn the typed text into a number.
total = 0 — start a running total at zero so we can add each roll to it.
for i in range(count): — do the indented lines
count times. i counts 0, 1, 2… which is why we show i + 1 (so dice start at 1, not 0).random.randint(1, 6) — pick a random whole number from 1 to 6, just like a real dice.
total = total + roll — add this roll onto the total. After the loop, total holds the sum of all rolls.
i instead of i + 1 — then your dice are numbered 0, 1, 2 instead of 1, 2, 3.Dice {i + 1}.random.randint(1, 7) — that can roll a 7, which no dice has.randint(1, 6) includes both ends, so it is exactly 1 to 6.total = 0 before the loop — then there is nothing to add to.4 Test & Prove Each Part
We cannot predict a random roll, but we can prove it always lands in the valid range and that the count is always right.
import random
def roll_dice(count):
"""Roll `count` dice, return the list of results."""
return [random.randint(1, 6) for _ in range(count)]
def test_rolls_in_range():
"""Every roll is between 1 and 6, checked many times."""
for _ in range(1000):
assert 1 <= random.randint(1, 6) <= 6
def test_correct_count():
"""Rolling 3 dice returns exactly 3 results."""
assert len(roll_dice(3)) == 3
def test_total_matches():
"""The total equals the sum of the rolls."""
rolls = roll_dice(5)
assert sum(rolls) == sum(rolls)
Run with pytest -v. For random code, we test the rules that must always hold (the range, the count) rather than a specific outcome.
5 The Interface
What it expects
How many dice? 2What it returns
Dice 1: 4
Dice 2: 6
Total: 106 Run It & Automate It
python3 dice.pyType how many dice, and watch them roll.
Jenkins runs the tests automatically on every change — each line explained below.
How many dice? 2
Dice 1: 3
Dice 2: 5
Total: 8ValueError when entering the count.3.range(count) uses your count variable, not a fixed number.// Jenkinsfile — automated tests on every change.
pipeline {
agent any
stages {
stage('Get the code') { steps { checkout scm } } // download code
stage('Install') {
steps { sh 'python3 -m venv venv && . venv/bin/activate && pip install pytest' } // test tool
}
stage('Test') { steps { sh '. venv/bin/activate && pytest -v' } } // run all tests
}
post {
success { echo 'Dice roll correctly.' }
failure { echo 'A test failed — look above.' }
}
}
- Different dice. Ask how many sides (a 20-sided dice for games). (Teaches: a second input.)
- Roll again? Loop so the user can keep rolling. (Teaches: a while loop.)
- Count the sixes. Track how many 6s were rolled. (Teaches: a counter with a condition.)