Computer 2

Monday, April 20, 2026

More Turtle Functions

Agenda

  1. Look at the following example program and see how these new functions work.
  2. Consider using these functions in your picture program.
  3. Finish and turn in your picture program by end of class. It should be saved as lastname_turtle_pic.py and put in your shared folder.

More Turtle Functions

Optional parameters and turning off animation.

Look at the following code. The included functions are more complicated than the ones we saw before. They make use of optional parameters, which are defined with what are called "default values." The default value is in the function definition list of parameters using the equal sign. When you use the function, you can choose to not provide values for the optional parameters, and then they will be given the default value. However, if you provide a value, the value you provide is used instead of the default value. When you use the function, you can name the parameter when you provide a value.

One other thing to notice is the command tracer(0), which turns off turtle animation. This allows your program to operate at full speed because it doesn't show the motion of the turtle. This is something you might try in your own programs.

from turtle import *
from math import sqrt

def right_triangle(leg_size, color, fill=False, turn_left=False):
    pencolor(color)
    if fill:
        fillcolor(color)
        begin_fill()
        
    forward(leg_size)
    if (turn_left):
        left(90)
    else:
        right(90)
    forward(leg_size)
    if (turn_left):
        left(135)
    else:
        right(135)
    hypotenuse = sqrt(2 * leg_size ** 2)
    forward(hypotenuse)
    if (turn_left):
        left(135)
    else:
        right(135)
        
    if fill:
        end_fill()

def rectangle(width, height, color, fill=False):
    pencolor(color)
    if fill:
        fillcolor(color)
        begin_fill()
    for i in range(2):
        forward(width)
        right(90)
        forward(height)
        right(90)
    if fill:
        end_fill()

    
tracer(0)
right_triangle(100, "orange", fill=True)
right_triangle(100, "orange", fill=True, turn_left=True)
rectangle(50, 200, "green", fill=True)

Class Assignment

Finish your turtle picture program and turn it in by the end of class today. Save your file as lastname_turtle_pic.py and put it in your shared folder.