Skip to content

Dice Roller and Turtle Spirals Revisited

Three short programs that lean on lists and one very useful operator. You'll roll dice, then come back to the spiral and color it two new ways — one random, one that marches through a list in order.


Overview

You'll write a dice-rolling function, then revisit the spiral to pick colors from a list — first randomly, then sequentially using the modulus operator.

You've got it when…

  • roll_dice(sides) returns a random number from 1 to sides.
  • Your random_spiral.py picks a random color for each move.
  • Your sequential_spiral.py steps through the color list in order, repeating.

Collaboration & AI

Work: On your own. Compare with a neighbor, but write your own code.

AI — AIAS Level 1, No AI: The modulus trick is the lesson; work it out yourself. What the levels mean.


Rolling Dice

  1. Create a new Python file, dice.py.
  2. Write a function roll_dice(sides) that returns a random number between 1 and sides.
  3. Write a main program that calls roll_dice with different inputs. For example:
    • Roll a 6-sided die: roll_dice(6)
    • Roll a 20-sided die: roll_dice(20)

Turtle Spiral Revisited

Here is the basic code for the two-colored spiral you made before.

import turtle
turtle.bgcolor("black")
turtle.speed(15)

for i in range(600):
    if i < 300:
        turtle.pencolor("maroon")
    else:
        turtle.pencolor("blue")
    turtle.forward(i)
    turtle.right(121)

Copy this code to a new file, random_spiral.py. Modify it to choose a random color from the list below for each turtle move, instead of switching between just two colors.

These are some of the color strings Python knows:

colors = ["red", "blue", "green", "yellow", "orange", "purple",
          "pink", "brown", "black", "white", "gray"]

A spiral where every line is a random color.


Sequential Spiral Colors

Copy your Section 2 code to a new file, sequential_spiral.py. Modify it to select colors from the list sequentially rather than randomly.

Hints

  • Recall list indexes (see Loop Through the Index Numbers on this page).
  • Consider the modulus operator (see this page, and try it).
  • Get the length of a list with len.
  • Think about using the modulus operator with the loop index i and the list length to walk the colors in order, over and over.

A spiral with a repeating pattern of colored lines.


Turn It In

  • dice.py
  • random_spiral.py
  • sequential_spiral.py

How It's Graded

This lab is worth up to 4 points. One score covers everything you turn in.

Score What it looks like
4 — Excellent All three files work: roll_dice(sides) returns 1 through sides for any die you hand it, random_spiral.py picks a fresh random color every move, and sequential_spiral.py walks the color list in order and wraps around using the modulus operator with the loop index and len() — the trick, visible in one line.
3 — Above Average All three run, with a slip — a die that can roll 0, or a sequential spiral that cycles correctly but hard-codes the list length.
2 — Average Two of the three programs work, or the "sequential" spiral is secretly random or still two-colored.
1 — Below Average One program works, or none run.
0 — Failing Nothing submitted, or no evidence of the three programs.