← All missions

🎲 Mystery Powers

πŸ” Repetition & Automation

1) What do I need to do?

🎯 Your mission: Dice duel

Roll a dice (1-6) for the hero and one for the villain. Print both rolls.

Use if/elif/else to print who wins β€” or if it's a draw!

2) What do I need to know?

Importing a library

A library is a box of extra commands someone else wrote. import random brings in commands for surprises!

random.randint(1, 6) picks a whole number from 1 to 6 β€” like rolling a dice. Both ends are included.

Run the example a few times β€” you'll get different results!

🧰 Your toolkit for this mission

Everything you need to know to finish this lesson on your own:

import

Brings in a library β€” a box of extra commands.

How to write it: Write import random on the first line.

import random

random.randint(a, b)

Picks a random whole number from a to b (both included).

How to write it: Store it in a variable.

roll = random.randint(1, 6)
print(roll)

random.choice(list)

Picks one random thing from a list.

How to write it: Put the options in square brackets, separated by commas.

coin = random.choice(["Heads", "Tails"])

Counting

Keep a total that goes up by 1 each time something happens.

How to write it: Start at 0 before the loop, add 1 inside an if.

heads = 0
for i in range(10):
    if random.choice(["Heads", "Tails"]) == "Heads":
        heads = heads + 1
print(heads)

Think about it

Look at this code. What happens next, and why? Say it in your own words.

print(roll)
Watch & explore β–Ά

Power-up: Importing a library

See it happen

1 / 3
import random
πŸ“¦ random
🎲 tools

Import opens a toolbox of extra commands.

3) Run the code & terminal output

Code Lab 🐍Playground β€” experiment freely!
Hero Terminal

Press β–Ά RUN and your program's messages appear here!

Training Arena
Training Arena
Your superhero companion

Captain Py

Our next adventure is yours to create.