Press ▶ Run to see what your program does.
Lesson 40: Import a toolbox (random)
Level 5, Python Thinks and Repeats. Bring in a module and roll dice with random. The new word: module. It follows lesson 39, Functions. Next comes lesson 41, Project: Guess the Number.
What the lesson explains
Toolboxes. import brings in a module full of ready-made tools.
Roll a dice. random.randint(1, 6) picks a number from 1 to 6. On the card: import random
What you do first
Roll it yourself. Roll a dice. One way to write it: import random / roll = random.randint(1, 6) / print("You rolled", roll). It prints: You rolled 1.
The code on the screen
Roll a dice. The editor shows 3 lines: import random / roll = random.randint(1, 6) / print("You rolled", roll). Run it, and it prints "You rolled 1".
What you practise
Try it, "Roll it yourself": Roll a dice. One way to write it: import random / roll = random.randint(1, 6) / print("You rolled", roll). It prints: You rolled 1.
Try it, "Pick from a list": Print a random move. The editor opens with: import random / moves = ["rock", "paper", "scissors"]. One way to write it: import random / moves = ["rock", "paper", "scissors"] / print(random.choice(moves)). It prints: rock.
Fix it, "Import first": Fix the NameError. The editor opens with: n = random.randint(1, 10) / print(n). One way to write it: import random / n = random.randint(1, 10) / print(n). It prints: 1.
What PlayBuddy and PlayType say
PlayType: "Python comes with toolboxes. Import one, and you get new tools!"
PlayBuddy: "The random toolbox picks numbers for you, like rolling dice."
Help in every lesson
Everything happens on a pretend computer in your browser, so nothing touches your real files. PlayBuddy and PlayType stand beside the board, cheer you on and say when it is your turn.
A green ring shows where to click the first time. After that you do it on your own, and if you slip twice the ring comes back.
Stars
The lesson has 3 things to do. Up to one slip in ten gives three stars, up to one in four gives two, and finishing always gives at least one. Stars and progress are saved in this browser only; there is no account.
About DayOneCode
DayOneCode is an independent learning site, not affiliated with or endorsed by Anthropic. Claude and Claude Code are trademarks of Anthropic.