Lab 2: Notes

Follow the Specifications

Follow the specifications carefully! Make sure you have all of the required functions (with the required parameters). It is OK (and encouraged) to go beyond the specification but make sure when you do so, you still satisfy the specifications in the lab.

Coding Best Practices

Considerations for your Lab 2 submissions:

  1. DRY it up! Whenever you find yourself duplicating code, think “function”!

    For example, if your triangle, polygon functions start like this…

     def triangle(x, y, side_length):
         penup()
         goto(x, y)
         setheading(270)
         pendown()
         ...
    

    with the same code for initializing the position of the turtle copied in each function, think of making a function that encapsulates that functionality for reuse, instead of copying the code.

     def move_without_pen(x, y, heading):
         penup()
         goto(x, y)
         setheading(heading)
         pendown()
    
    
     def triangle(x, y, side_length):
         move_without_pen(x, y, 270)
         ...
    
  2. Constants! There are many opportunities for constants in Lab 2, particularly around setting and using the screensize. Like variables, constants in Python can be used in arithmetic expressions, further increasing their usefulness. For example:

     SCREEN_RANGE_IN_PIXELS=350
     setup(2*SCREEN_RANGE_IN_PIXELS, 2*SCREEN_RANGE_IN_PIXELS, 0, 0)
    
     ...
        
     x = randint(-SCREEN_RANGE_IN_PIXELS, SCREEN_RANGE_IN_PIXELS)
     y = randint(-SCREEN_RANGE_IN_PIXELS, SCREEN_RANGE_IN_PIXELS)
    
  3. Make sure that your functions work regardless of context. For example, be sure to set the heading of the turtle explicitly in the triangle function. Recall we always want the left edge to be vertical. Don’t rely on the state of the turtle always being maintained as vertical outside the function. If the invariant is not maintained then your triangle function will not do the “right thing”. Similarly, don’t assume that the pen will be up at the beginning of a drawing function. In general we want to make sure our functions work regardless of what has happened before. And so we should set the position and orientation to be exactly what we need without relying on any assumptions.

  4. Parameters! Whenever you find yourself copying a function to make a small change, e.g., background color, make a parameter instead. Similarly, instead of writing code such as:

     fillcolor("brown")
     begin_fill()
     square(0, 0, 100)
     end_fill()
        
     fillcolor("red")
     begin_fill()
     square(200, 200, 100)
     end_fill()
    

    The “filling” code would be better implemented within the square function:

     def square(x, y, length, color):
         move_without_pen(x, y, 0)
         fillcolor(color)
         begin_fill()
         for i in range(4):
             forward(length)
             right(90)
         end_fill()
    

    with “brown” and “red” as arguments, e.g.,

     square(0, 0, 100, "brown")
     square(200, 200, 100, "red")