Professional Documents
Culture Documents
Code HS
Code HS
Turtle Commands
Commands are how we tell Tracy the Turtle to do things.
Movement
# The forward command makes Tracy move forward a given distance
forward(10)
forward(50)
forward(200)
Drawing Circles
# The circle command makes Tracy draw a circle above her with specified radius
circle(10)
circle(100)
# Another parameter can be used to control the number of points in the shape
# This command will draw a triangle
circle(10, 360, 3)
Pen
https://codehs.com/editor/documentation/turtle Page 1 of 10
CodeHS 17/07/2023, 12:07
Rotation
# Tracy will turn 90 degrees to the left
left(90)
# We can use the `setheading` command (or `seth`) to set Tracy to a specific direction
Position
Tracy is positioned in an (x, y) coordinate plane like so:
https://codehs.com/editor/documentation/turtle Page 2 of 10
CodeHS 17/07/2023, 12:07
Each point in Tracy's world can be located by an (x, y) coordinate. For example:
Speed
We can set Tracy's speed using the speed command
https://codehs.com/editor/documentation/turtle Page 3 of 10
CodeHS 17/07/2023, 12:07
# Slowest
speed(1)
# Normal
speed(6)
# Fast
speed(10)
Color
# Sets the background color of the canvas to purple
bgcolor("purple")
Filling
https://codehs.com/editor/documentation/turtle Page 4 of 10
CodeHS 17/07/2023, 12:07
Clear Screen
# To clear the screen of any markings, use clear command
clear()
Loops
Loops help us repeat commands which makes our code much shorter.
For Loops
Use for-loops when you want to repeat something a fixed number of times.
https://codehs.com/editor/documentation/turtle Page 5 of 10
CodeHS 17/07/2023, 12:07
# You can change the value of i inside a for loop by using mathematical expressions
for i in range(3):
forward(i*10)
While Loops
Use while loops when you want to repeat something an unknown number of time or until a condition becomes
false.
If there is no point where the condition becomes false, you will create an infinite loop which should always be
avoided!
https://codehs.com/editor/documentation/turtle Page 6 of 10
CodeHS 17/07/2023, 12:07
Functions
Writing a function is like teaching Tracy the Turtle a new word.
Naming Functions: You can name your functions whatever you want, but you can't have spaces in the function
name. Instead of spaces, use underscores (_) like_this_for_example
Make sure that all the code inside your function is indented one level!
Defining a Function
We define a function to teach Tracy the instructions for the new word.
def name_of_your_function():
# Code that will run when you make a call to
# this function.
# EXAMPLES
https://codehs.com/editor/documentation/turtle Page 7 of 10
CodeHS 17/07/2023, 12:07
Calling a Function
We call a function to get Tracy to actually carry out the new command.
Variables
We use variables to store values that can be used to control commands in our code. The variable values can
also be altered throughout the code.
User Input
https://codehs.com/editor/documentation/turtle Page 8 of 10
CodeHS 17/07/2023, 12:07
# Ask the user for input and save it to a variable to be used in code
# This will only work if the input is being used as a string
circle_color = input("What is the color of the circle? ")
If/Else Statements
We can tell Tracy how to make decisions using if/else statements.
Make sure that all the code inside your if/else statement is indented one level!
If Statements
Use an if statement to tell Tracy to do something only when a condition is true. If the condition is false, Tracy
will skip the commands indented underneath.
# Tracy will only draw a circle if the count variable is less than 5
if count < 5:
circle(50)
If/Else Statements
Use an if/else statement to force Tracy to make a decision between multiple conditions. If the first condition is
false, Tracy will skip to the next condition until she finds one that is true. If no conditions are true, the
commands inside the ‘else’ block will be performed.
Math operators
Addition +
Subtraction -
https://codehs.com/editor/documentation/turtle Page 9 of 10
CodeHS 17/07/2023, 12:07
Multiplication *
Division /
Modulus %
Exponent **
Comparing values
Equal to ==
Not Equal to !=
Greater Than >
Greater Than or Equal to >=
Less Than <
Less Than or Equal to <=
Comments
We use comments to leave notes about the code to the reader. Comments are not actually run by Python, they
are just there to help us read the code.
We can make multiline comments with """ and single line comments with #
"""
A multi-line comment describes your code
to someone who is reading it.
"""
https://codehs.com/editor/documentation/turtle Page 10 of 10