🎲
Coding🔬 Ages 11-13Beginner 10 min read

Build a Dice Roller in Python

Code a virtual dice roller in Python. Roll one die or many, choose how many sides, add up the total and show ASCII dice faces. Includes complete runnable code, a sample run, key ideas and a quiz.

Key takeaways

  • random.randint(1, 6) simulates one roll of a six-sided die
  • A for loop rolls many dice and a running total adds up the result
  • You can change the number of sides to make a d6, d20 or any die you like
  • Functions let you reuse your roll code instead of copying it
  • A dictionary can map a number to a printable dice face

Roll the dice with code

Lost the dice from your board game? Build your own! A dice roller is a quick, satisfying Python project that teaches randomness, loops and functions. We'll start with a single roll, then roll many dice, let the player choose the number of sides, and even print little dice faces.

If you've met random numbers and games in Python, you already know the key tool. Let's roll.

Step 1: roll one die

A six-sided die shows 1, 2, 3, 4, 5 or 6. random.randint(1, 6) gives exactly that:

import random

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

randint includes both ends, so 1 and 6 are both possible — just like the real thing.

Step 2: make it a function

We'll roll dice many times, so let's wrap it in a reusable function. A function with a parameter lets us choose the number of sides too:

import random

def roll_die(sides=6):
    return random.randint(1, sides)

print(roll_die())     # a normal d6
print(roll_die(20))   # a 20-sided die

sides=6 is a default — call roll_die() and you get a six-sided die; call roll_die(20) for a d20. (New to functions? See python functions and parameters.)

Step 3: roll many dice and total them

To roll several dice, loop and keep a running total:

total = 0
for i in range(3):          # roll 3 dice
    r = roll_die()
    print("Die", i + 1, "shows", r)
    total += r
print("Total:", total)

total += r adds each roll onto the total. (For more on repeating, see python for loops.)

Step 4: show a dice face

A plain number is fine, but a little drawing is nicer. We can map each number to an ASCII face with a dictionary:

faces = {
    1: "[     •     ]",
    2: "[ •       • ]",
    3: "[ •    •  • ]",
    4: "[ • •   • • ]",
    5: "[ • • • • • ]",
    6: "[ •••   ••• ]",
}
print(faces[roll_die()])

The dictionary looks up the rolled number and gives back its picture.

The complete dice roller

Here's the finished program. It asks how many dice to roll and how many sides each has, then shows every roll and the total:

import random

def roll_die(sides):
    return random.randint(1, sides)

print("=== DICE ROLLER ===")

while True:
    sides = int(input("How many sides per die? "))
    count = int(input("How many dice? "))

    total = 0
    print("Rolling...")
    for i in range(count):
        r = roll_die(sides)
        print(f"  Die {i + 1}: {r}")
        total += r

    print(f"Total of {count}d{sides} = {total}")

    again = input("Roll again? (yes/no): ").lower()
    if again != "yes":
        print("Goodbye!")
        break

How it works:

  1. The player types the number of sides and the number of dice. int(input(...)) turns the text into numbers.
  2. The for loop runs count times, calling roll_die(sides) each time and adding the result to total.
  3. {count}d{sides} prints in standard game notation — 3d6 means three six-sided dice.
  4. The outer while True: loop lets the player roll again or quit.

A sample run:

=== DICE ROLLER ===
How many sides per die? 6
How many dice? 2
Rolling...
  Die 1: 4
  Die 2: 5
Total of 2d6 = 9
Roll again? (yes/no): no
Goodbye!

Try it yourself

Extend your roller:

  • Show the faces. For six-sided dice, use the faces dictionary above to print a picture next to each roll.
  • Count the rolls. Roll 6000 dice in a loop and use a list or dictionary to count how often each number appears. Are they roughly equal? They should be!
  • Highest wins. Roll for two players and announce who got the higher total.
  • Reroll on doubles. If both dice match, print "Doubles! Roll again" and add a bonus roll.

Quick quiz

Test yourself and earn XP

Which line rolls a single six-sided die?

To roll three dice and add them up, you would...

How do you make a 20-sided die (d20)?

Why put the rolling code inside a function?

What does total += roll do?

FAQ

random.randint gives each face an equal chance, so yes — over many rolls every number from 1 to 6 should appear roughly the same number of times. The numbers are 'pseudo-random', made by a maths formula, which is perfectly fair for games. Try rolling 6000 times and counting each face to see the balance.

Ask the player how many dice and how many sides, then loop that many times calling random.randint(1, sides). Store each roll in a list so you can print every die and the total. This is exactly how tabletop games describe rolls such as '2d6' (two six-sided dice).