This program will draw 9 shapes from 0 points to 9 points in a line.

As you explore this example, test out the following:

If the letter i changes to a in the for loop definition, where does it also need to be changed to keep the same functionality?
If you only wanted to draw shapes with 0-5 points, what would need to be changed in the program?
Are we able to use i in the setposition command on line 8, like so: setposition(-180, i)? Explain the outcome and why you think this is.

1 answer

To answer your questions, let’s first consider a basic structure of the program you might be referring to. Below is an example of how such a program might look in Python, using a module like turtle to draw shapes:

import turtle

def draw_shape(points):
    angle = 360 / points
    for _ in range(points):
        turtle.forward(50)  # Length of each side of the shape
        turtle.left(angle)

turtle.speed(0)  # Fastest drawing speed
for i in range(10):  # Loop from 0 to 9
    turtle.penup()
    turtle.setposition(-180 + i * 40, 0)  # Move to the appropriate position
    turtle.pendown()
    draw_shape(i)  # Draw shape with i points
turtle.done()

Questions Answered:

  1. Changing i to a:
    If you change the loop variable from i to a in the for loop definition (i.e., for a in range(10):), you'll need to update every instance of i in the rest of the program to a as well. Specifically, you would change:

    • turtle.setposition(-180 + i * 40, 0) to turtle.setposition(-180 + a * 40, 0)
    • draw_shape(i) to draw_shape(a)

    This ensures that you're consistently using the same variable name throughout the loop.

  2. Drawing shapes with only 0-5 points:
    To limit the shapes being drawn to 0-5 points, you would change the range in the for loop from range(10) to range(6):

    for i in range(6):  # Loop from 0 to 5
    
  3. Using i in setposition:
    If you were to use i in the command turtle.setposition(-180, i), it would not work as intended. The setposition function expects coordinate values (x, y) as arguments. In this case, if i is at a point where it varies (0-9), it might move the turtle vertically instead of horizontally, which is likely not what you want for this drawing. Each shape is intended to be drawn in a line horizontally, so using i for the x-coordinate (horizontal position) and having a fixed y-coordinate (like 0) maintains the horizontal line symmetry.

    Using i in the form setposition(-180 + i * 40, 0) is appropriate because it modifies only the x-coordinate and keeps the y-coordinate static, allowing shapes to be arranged side by side without overlapping.

In summary, you need to keep variable names consistent, modify your range for drawing fewer shapes, and carefully consider how you position each shape to maintain the desired layout.