Skip to main content

Exercises 20.8 Chapter Exercises

1.

The code currently draws a square. Change it so that it draws a triangle. Do so by modifying the loop and the turn that the turtle makes without modifying other code.
Remember that to make a complete shape, the turns must sum to 360 degrees.

2.

Fix the errors in the code so that it draws an octagon. Do so by fixing the issues in the loop-based code. There are three issues to fix.
Remember that to make a complete shape, the turns must sum to 360 degrees.

3.

Fix the indention in the code below to correctly draw 20 pentagons (5-sided polygons).
Make sure to use four spaces for each indentation level (the tab key will automatically use 4 spaces).

4.

    Which is range recipe would generate the sequence: 10, 8, 6, … 2
  • range(10, 0, -2)
  • Correct
  • range(10, 1, 2)
  • That counts up by 2’s
  • range(10, 2, -2)
  • That recipe would not generate 2 because it is the stop value
  • range(10, 3, 2)
  • This recipe counts up by 2’s

5.

Write the correct recipe to fill in the ____________ in the code below to generate the pattern 5, 25, 45, … 105. Use the smallest possible value for your stopValue.
range(____________)
Just write what would replace the blank below. (Don’t include “range” or the parentheses in your answer; do include commas).

6.

We want to make a series of rectangles such that the first one is 10x90, the next is 20x80, the next is 30x70, … until we draw one that is 90x10. Doing so will make a “staircase grid” like the one shown below:
Below is a program with rectangle already defined. Add code that uses a for loop to call the procedure with the correct values for its parameters. Make sure that the 90-width and 10-height rectangle is the first one you draw. You should only need one loop and it must count down from 90 to 10.
Hints:
  • Start by writing a loop that just prints out the values 90, 80, 70, …, 10. These will be the widths of the rectangles you end up drawing.
  • Next, for each width, calculate the corresponding height. Each width/height pair should always add to 100, so if we call the width \(w\text{,}\) the height must be \(100 - w\text{.}\) Print out the height with the width so that your output looks like 90 10, 80 20, 70 30, …, 10 90.
  • Finally, call rectangle in your loop and pass it the width and height you are calculating.
The autograder will not verify everything about your program. It is up to you to determine if your program is correct or not.
You have attempted of activities on this page.