|
| 1 | +--- |
| 2 | +layout: class |
| 3 | +title: "COMP 151, Spring 2024, Lab 03" |
| 4 | +semester: s24 |
| 5 | +--- |
| 6 | + |
| 7 | +## Lab 03: Experiments with Turtle |
| 8 | + |
| 9 | +Our book has some nice content using `turtle`, such as [this |
| 10 | +activity](https://runestone.academy/ns/books/published/monmouth-comp151-spring25/Projects/drawing_a_circle.html#drawing-a-circle), |
| 11 | +but doesn't involve enough problem solving with it. |
| 12 | + |
| 13 | +So, I created a custom lab which involves `turtle`, `random`, and just |
| 14 | +a bit of problem solving. |
| 15 | + |
| 16 | +### Instructions |
| 17 | + |
| 18 | +Choose any code block from the book. Something from the [Drawing a |
| 19 | +Circle with the |
| 20 | +Turtle](https://runestone.academy/ns/books/published/monmouth-comp151-spring25/Projects/drawing_a_circle.html#drawing-a-circle) |
| 21 | +activity would be fitting, but it doesn't really matter. |
| 22 | + |
| 23 | +You need to write code that satisfies the following requirements: |
| 24 | + |
| 25 | +- Randomly choose a number between 1 and 10 (inclusive), call it $n$. |
| 26 | +- In a loop, create $n$ turtles. |
| 27 | +- For each turtle: |
| 28 | + - Assign them a random color. |
| 29 | + - Choose a random radius from 50, 75, 100, 125, 150 |
| 30 | + - Have the turtle draw a circle with that radius and color |
| 31 | +- At the end, print total and average distance travelled |
| 32 | + |
| 33 | +**Note**: Each turtle should choose its color and radius independently |
| 34 | +of the other turtles. |
| 35 | + |
| 36 | +When you are done, raise your hand and show me your code. |
| 37 | + |
| 38 | +### Hints |
| 39 | + |
| 40 | +- In addition to our book's chapters on `turtle` and `random`, you |
| 41 | + will probably need to skim Python's official documentation for the |
| 42 | + [`turtle` module](https://docs.python.org/3/library/turtle.html) and |
| 43 | + for the [`random` |
| 44 | + module](https://docs.python.org/3/library/random.html). |
| 45 | +- To draw a circle, learn about [`turtle`'s `circle` |
| 46 | + method](https://docs.python.org/3/library/turtle.html#turtle.circle). |
| 47 | +- You will need just a little bit of [chapter |
| 48 | + 6](https://runestone.academy/ns/books/published/monmouth-comp151-spring25/Sequences/toctree.html) |
| 49 | + content to create a list of possible radii, and maybe a list of |
| 50 | + possible colors. Luckily, we'll cover enough of that right before |
| 51 | + lab. |
| 52 | +- To choose an item randomly from a list, learn about [`random`'s |
| 53 | + `choice` |
| 54 | + method](https://docs.python.org/3/library/random.html#random.choice). |
| 55 | +- To figure out the area a turtle traveled, you'll need to use the |
| 56 | + formula for the circumference of a circle. |
| 57 | +- To compute the **total** distance all turtles have traveled, you'll |
| 58 | + need to have a separate variable that starts at 0 and gets added to |
| 59 | + inside your loop. This is your first example of the *accumulator* |
| 60 | + pattern. Don't be afraid to ask for help about this. |
| 61 | + |
0 commit comments