Put randomness to work. Roll a die a thousand times and count the sixes — is it fair? — then build a magic 8-ball or rock-paper-scissors. Randomness meets the accumulator, and it’s the on-ramp to Unit 4 simulations.
No reading — build. Keep 3.17 (random), 3.4 (accumulator), and 2.13 (elif) open. Pick one program below to build fully.
Still in Programs, late Week 4. Randomness in a loop with a counter is your first simulation — run a chance experiment many times and measure what happens. That’s literally what Unit 4 opens with.
Build one of these (a second if you have time):
□ Dice experiment — roll a die 1000 times, count how many are sixes, and print the count and the fraction. Then ask: is it close to 1⁄6? (accumulator + random)
□ Magic 8-ball — pick a random answer from a list of replies and print it. (random + lists)
□ Rock-paper-scissors — the computer picks randomly, you compare with an elif chain and report win/lose/tie. (upgrade of the 2.15 idea)
□ A purpose sentence comment at the top.
□ At least one call to random (randint).
□ A loop with an accumulator/counter (dice), OR a list (8-ball), OR a conditional (RPS).
This is a simulation in seven lines: repeat a random trial many times and count the outcome you care about.
# Counts sixes in 1000 rolls to # check whether the die is fair. import random sixes = 0 for i in range(1000): roll = random.randint(1, 6) if roll == 6: sixes = sixes + 1 print(sixes, sixes / 1000)
# purpose: what + why # # bring in random # accumulator: initialize # run 1000 trials (count loop) # one random roll per trial # selection: is it a six? # count it (update) # use: expect about 1/6 ≈ 0.167
Hover or tap a line to light its twin. Every ingredient of Unit 3 is here: a library, a loop, a conditional, and an accumulator. A fair die should land near 1000 ÷ 6 ≈ 167 sixes.
Roll the die many times and watch the count of sixes. Run it a few times: the exact number jumps around, but the fraction settles near 1⁄6. That’s a simulation in action.
□ The working program (uses random, runs correctly).
□ Your purpose sentence at the top.
□ One sentence on what you observed — e.g. “across runs the six-count changed but the fraction stayed near 1⁄6” — and whether that matches a fair die.
That observation sentence is the start of Unit 4: using a simulation to answer a question (“is it fair?”) instead of just producing output.
Run your dice loop for just 10 rolls at first and
print each roll, so you can see it working and confirm the counter increments only on
sixes. Then bump it to 1000 and remove the per-roll printing. Debugging a loop is far easier at 10
than at 1000.
Solo or paired — if you pair, swap every 10 minutes; both of you should be able to point to the accumulator and the random call.
Count every face, not just sixes: use a list of six counters and
tally each roll, then check they’re all near 167 — a preview of Unit 5’s
frequency tables. Or, for rock-paper-scissors, add a running score across rounds and play best-of-5.
Or turn the dice experiment into a procedure count_sixes(n) that takes the number of
rolls as a parameter and returns the count — the Create PT shape, again.