Skip to content

Functions

Deprecated Lab — Trinket is shutting down

This lab was written for Trinket.io, which is shutting down in August 2026. The embedded trinket.io links on this page will stop working after that date.

These pages are kept for reference only. The current version of this course now runs every lab as an inline Skulpt editor right in the page — no account or install needed. Start at Chapter 1: Welcome to Python.

Functions

Now we will create a new function that will draw each side.

Sample Code

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
20
21
22
import turtle
dan = turtle.Turtle()
dan.shape('turtle')

distance = 50
angle = 90

def side():
   # if event then red 2, 4 etc.
   if i % 2:
      dan.color('red')
   # else if odd then draw blue 1, 3 etc.
   else:
      dan.color('blue')
   dan.forward(distance)
   dan.right(angle)

# repeat the forward/right functions four times
for i in range(4):
   side()

dan.write('done with square')

Run the Example on Trinket

Run the Functions Program on Trinket

Experiments

Can you change the name of the function to be "petal"?