Python Classroom
  • Introduction
  • About Python Classroom
  • Python Cloud Options
    • CS50 IDE
      • CS50 IDE Overview
      • CS50 IDE and Python
      • CS50 IDE Debugging
      • CS50 IDE Shortcuts
      • Linux Bash Shell
      • Vim Mode
        • Vim Tutorial
    • CS50 Sandbox
    • PythonAnywhere
    • Repl.it
  • Python Curriculum Map
    • Pedagogy
      • Python Professional Development
      • Teaching Tips
      • Assessment Tips
      • Rubrics
      • Activities
        • Picture Activity
        • Map Activity
        • Crossing the Bridge
        • NIM
        • Mastermind
        • Cards
          • Card Deck Algorithms
          • Sorting Cards
        • River Crossing
          • Jealous Boyfriends
          • Cannibals and Priests
          • Family
          • Police and the Thief
          • Humans and Monkeys
          • Moving Money
        • Crossing the River
        • Traveling Salesperson
        • Logic Problems
        • CIA Crack the Code
        • IQ Test
        • Puzzles
    • AP Computer Science Principles Framework
  • Python Philosphy
    • How to Practice Python
  • microbit
  • Turtle Graphics
    • Turtle Examples
    • Turtle Activities
    • Turtle Maze Problems
    • Turtle Graphics with loops
    • Turtle Snake
    • Turtle Graphics with Conditionals
  • Output
    • Output Examples
    • Output Mistakes
  • Variables
    • Variable Data Type Examples
    • Variable Role Examples
    • Variables Mistakes
    • Variables Problems
  • Math
    • Math Examples
    • Math Mistakes
    • Math Problems
    • Math Self Check
  • Input
    • Input Examples
    • Input Mistakes
    • Input Problems
  • Decisions
    • if
      • if Examples
      • if Mistakes
      • if Problems
    • if else
      • if else Examples
      • if else Problems
      • if / if else Problems
    • if elif else
      • if elif else Examples
      • if elif else Problems
    • nested if
      • nested if Examples
      • nested if Problems
    • Logical Operators
      • Logical Operators Examples
    • Adventure Game Project
  • Loops
    • while loop - count up
      • Examples
      • Problems
    • while loop - countdown
      • Examples
      • Problems
    • while loop - sentinel value
      • Problems
    • while loop - sentinel menu
      • Problems
    • for loop - range (one argument)
      • Examples
      • Problems
    • for loop - range (two arguments)
      • Problems
    • for loop - range (three arguments)
      • Problems
  • Lists
    • Lists - Numbers
      • Problems
    • Lists - Strings
      • Problems
      • Shopping Project
  • Dictionaries
  • String Methods
  • Functions
    • Variable Scope
    • Functions - no parameters
    • Functions - one parameter
    • Functions - return
    • Functions - lists
  • Files
  • Classes
    • Inheritance
  • Python Projects
    • Adventure Game
    • Restaurant Project
    • Trivia Game
    • Family Tree Project
  • Raspberry Pi
    • Raspberry Pi Models
    • Raspberry Pi Setup
  • Roblox
  • Glossary
Powered by GitBook
On this page
  • Shapes
  • Square
  • Rectangle
  • Zigzag
  • Star
  • Spiral
  • Loops
  • Square
  • Spiral
  • Polygons
  • Star
  • Spiral Circle

Was this helpful?

  1. Turtle Graphics

Turtle Graphics with loops

PreviousTurtle Maze ProblemsNextTurtle Snake

Last updated 6 years ago

Was this helpful?

Shapes

To start, let's try some basic designs without loops.

Square

square.py
import turtle
turtle.showturtle()
turtle.shape("turtle")
turtle.forward(50)
turtle.right(90)
turtle.forward(50)
turtle.right(90)
turtle.forward(50)
turtle.right(90)
turtle.forward(50)
turtle.right(90)

To add color to your design, wrap the following lines of code before and after the turtle movements.

turtle.fillcolor("green")
turtle.begin_fill()
turtle.end_fill()
import turtle
turtle.showturtle()
turtle.shape("turtle")
turtle.fillcolor("green")
turtle.begin_fill()
turtle.forward(50)
turtle.right(90)
turtle.forward(50)
turtle.right(90)
turtle.forward(50)
turtle.right(90)
turtle.forward(50)
turtle.right(90)
turtle.end_fill()

turtle.pencolor("red") # this statement changes the pen's color.

Rectangle

Let's draw a rectangle using variables. In Python, you name a variable and assign it a value. Replace each length and angle with a variable.

rectangle.py
import turtle
turtle.showturtle()
turtle.shape("turtle")
length1 = 100
length2 = 200
angle = 90
turtle.forward(length1)
turtle.right(angle)
turtle.forward(length2)
turtle.right(angle)
turtle.forward(length1)
turtle.right(angle)
turtle.forward(length2)
turtle.right(angle)

Zigzag

Star

zigzag.py
import turtle
length = 25
angle = 45
turtle.showturtle()
turtle.shape("turtle")
turtle.forward(length)
turtle.right(angle)
turtle.forward(length)
turtle.left(angle)
turtle.forward(length)
turtle.right(angle)
turtle.forward(length)
turtle.left(angle)
turtle.forward(length)
turtle.right(angle)
turtle.forward(length)
turtle.left(angle)
turtle.forward(length)
turtle.right(angle)
turtle.forward(length)

Spiral

spiral.py
import turtle
length = 10
angle = 90
turtle.showturtle()
turtle.shape("turtle")
turtle.forward(length+length)
turtle.right(angle)
length = length + 10
turtle.forward(length+length)
turtle.right(angle)
length = length + 10
turtle.forward(length+length)
turtle.right(angle)
length = length + 10
turtle.forward(length+length)
turtle.right(angle)
length = length + 10
turtle.forward(length+length)
turtle.right(angle)
length = length + 10
turtle.forward(length+length)
turtle.right(angle)
length = length + 10
turtle.forward(length+length)
turtle.right(angle)
length = length + 10
turtle.forward(length+length)
turtle.right(angle)
length = length + 10
turtle.forward(length+length)
turtle.right(angle)
length = length + 10
turtle.forward(length+length)
turtle.right(angle)
length = length + 10
turtle.forward(length+length)
turtle.right(angle)
length = length + 10
turtle.forward(length+length)
turtle.right(angle)
length = length + 10
turtle.forward(length+length)
turtle.right(angle)
length = length + 10
turtle.forward(length+length)
turtle.right(angle)
length = length + 10

Loops

Loops are used when you have a block of code that you want to repeat.

A for loop is used when you have a block of code which you want to repeat a fixed number of times. The for loop iterates through the block of indented code.

for x in range():

x is a variable that steps through values

<do something>

do something in the indented block

for.py
for x in range(4):
    print(x)
$ python for.py
0
1
2
3
4

Square

square2.py
import turtle
turtle.showturtle()
turtle.shape("turtle")
for x in range(4):
    turtle.forward(50)
    turtle.right(90)

Spiral

spiral2.py
import turtle
length = 10
angle = 90
turtle.showturtle()
turtle.shape("turtle")
for x in range(10):
    turtle.forward(length+length)
    turtle.right(angle)
    length = length + 10
spiral3.py
import turtle
angle = 91
turtle.showturtle()
turtle.shape("turtle")
for x in range(100):
    turtle.forward(x)
    turtle.left(angle)
circle.py
import turtle
angle = 91
turtle.showturtle()
turtle.shape("turtle")
for x in range(100):
    turtle.circle(x)
    turtle.left(angle)

Polygons

Shape

Sides

Interior Angle

Triangle

3

60

Quadrilateral

4

90

Pentagon

5

108

Hexagon

6

120

Heptagon

7

128.571

Octagon

8

135

Nonagon

9

140

Decagon

10

144

Hendecagon

11

147.273

Dodecagon

12

150

triangle.py
import turtle
length = 100
angle = 120
turtle.showturtle()
turtle.shape("turtle")
for i in range(3):
    turtle.forward(length)
    turtle.right(angle)

Star

Using a loop, we can produce an eight point star.

star1.py
import turtle
turtle.showturtle()
turtle.shape("turtle")
turtle.pencolor('green')
for x in range(13):
        turtle.forward(200)
        turtle.left(150)

What happens if we try changing the range and the left angle?

star2.py
import turtle
turtle.showturtle()
turtle.shape("turtle")
turtle.pencolor('purple')
for x in range(100):
        turtle.forward(200)
        turtle.left(175)

Spiral Circle

spiral.py
import turtle
turtle.showturtle()
turtle.shape("turtle")
turtle.pencolor('pink')
for i in range(180):
    turtle.forward(100)
    turtle.right(30)
    turtle.forward(20)
    turtle.left(60)
    turtle.forward(50)
    turtle.right(30)
    turtle.penup()
    turtle.setposition(0, 0)
    turtle.pendown()
    turtle.right(2)