×

python · level 2 · unit 5 · lesson 2

  1. meet it
  2. guess first
  3. your turn
  4. fix it
  5. nailed it

Tap a green word if you get lost.

meet it

The module rolls dice, and a makes it repeatable.

import random gives you chance. random.randint(1, 6) rolls a dice, and both ends count. random.choice(prizes) picks one item from a list, and random.shuffle(cards) mixes a list up where it is.

Computers fake chance with a recipe that starts from a seed. Call random.seed(7) first, and the rolls that follow are the same on every single run.

That sounds like cheating, but it is how you test a game: same seed, same rolls, same result to check.

import random
random.seed(7)
roll = random.randint(1, 6)
print(f"You rolled a {roll}")
random.seed(7)
print(roll == random.randint(1, 6))

Leave the seed out of a real game, so every game is different.