Lab 3 Bonus: Turtle Cake Baking (Functions with Python Turtles)
Our goal in this part of the lab is to write a collection of fruitful
functions that draw cakes using Python's turtles.
Open lab03/cake.py
to get started.
Task 1: Two layer cake
You are given a function called cake
that takes height and width and draws a two-layer cake. The bottom layer are the given height and width, and the top layer is half the width and the same height as the bottom layer.
cake(80, 400, 'red')
draws this cake and should return 48000:
cake(75, 200, 'blue'')
draws this cake and should return 22500:
A few notes about the cake
function:
- Note that the turtle always returns to its initial starting position at the end of the function. Make sure any edits that you make to this function still ensure that the turtle is back in its original position before the function was called.
- Right now, the
cake
function always returns 0. You will change this to calculate the total area of the two cake layers, and return that value from yourcake
function. The areas for the two cakes above are shown. - Why should the red cake above return 48000? The bottom layer has dimensions 80 x 400, which has area 32000. The top layer has dimensions 80 x 200, which as area 16000. 32000 + 16000 = 48000.
How to test?
You can test the cake
function in the Shell. Here are two examples.
Hint: if you see overlapping cakes in your Turtle canvas, you can use setupTurtle()
to clear the canvas between calls to your cake
function (as shown above).
Task 2: Three layer cake with a candle
Now, make changes to your cake
function so that it has a third layer. The third layer is the same height as the first two layers, and half the width of the second layer. Here are some sample 3-layer cakes:
cake(80, 400, 'red')
draws this cake and should return 56000:
cake(75, 200, 'blue'')
draws this cake and should return 26250:
Task 2A: Draw the third cake layer, and make sure your turtle is back in its original starting position when done. You'll know because you'll see the turtle in the center of the bottom cake layer.
Task 2B: Re-calculate the total area of the cake by adding in the area of the
third layer. Check that your cake
function returns the numbers shown above. Note we are not counting the area of the candle, only the area of the cake layers.
Task 2C: Add the candle to your third cake layer. You are given the candle
function which draws a blue candle with an orange flame. You specify the height of the candle; its width is fixed at 20. Here are two invocations of the candle function:
candle(45) | candle(100) |
Note
- The
candle
function does not return a value; it merely draws on the canvas. - The
candle
function puts the turtle back where it was before it was invoked.
Task 3: A triplet of three-layer cakes in a row
Write a new function called cakeRow
that draws 3 cakes in a row, left to right
where the rightmost edge of the first cake overlaps with the leftmost edge of the second cake. All cakes will have the same dimensions, and cakeRow
will return the total area of all 3 cakes. The cakes are always red, green and blue from left to right.
cakeRow(50, 280)
draws these cakes and should return 73500:
cakeRow(70, 100)
draws these cakes and should return 36750:
Check that your cakeRow
function returns the turtle to its starting position (centered in the bottom layer of the leftmost cake).
Task 4: A triplet of diminishing three-layer cakes
Write a new function called shrinkingCakeRow
that draws 3 cakes in a row, left to right. It is still the case that, within any cake, the layers decrease in width by 50% and the height of all layers is the same. However, across the 3 cakes in this row, now the width and height will decrease. The second cake's bottom layer is half the width and half the height of the first cake's bottom layer. And the third cake's bottom layer is half the width and half the height of the second cake's bottom layer.
shrinkingCakeRow(80, 300)
draws these cakes and should return 55125:
shrinkingCakeRow(40, 100)
draws these cakes and should return 9187.5:
Note:
- Check that your
shrinkingCakeRow
function returns the turtle to its starting position (centered in the bottom layer of the leftmost cake). - Note how the cakes are no longer aligned on the bottom edge
- Note that the fixed candle width of 20 is too wide for the shrinking cakes when the first width is 100 or less.
Yay, you made it to the end of the lab!
If you still have time left, there are some optional exercises you could work on:
- More turtle drawing will give more practice using parameters and writing turtle functions.
- Knowledge check linked at the bottom of the page in the Table of Contents. Take this fun quiz to check your knowledge.