Difference between revisions of "Turtle - List Assignment"
From WLCS
(6 intermediate revisions by the same user not shown) | |||
Line 12: | Line 12: | ||
from turtle import * | from turtle import * | ||
− | speed(0) # fastest turtle | + | #speed(0) # fastest turtle |
#create empty list of turtles | #create empty list of turtles | ||
Line 20: | Line 20: | ||
x = 0 | x = 0 | ||
while x < 10: | while x < 10: | ||
− | turtles | + | turtles.append(Turtle()) |
− | x = x + | + | x = x + 1 |
#do stuff with turtles | #do stuff with turtles | ||
Line 27: | Line 27: | ||
x = 0 | x = 0 | ||
while x < 10: | while x < 10: | ||
− | turtles.forward(x * | + | turtles[x].forward(x * 100) |
− | x = x + | + | x = x + 1 |
exitonclick() | exitonclick() | ||
Line 39: | Line 39: | ||
# Tell each turtle to go to a random location and draw a random circle | # Tell each turtle to go to a random location and draw a random circle | ||
# Tell each turtle to go to a random location and move forward a random distance between 50-200 | # Tell each turtle to go to a random location and move forward a random distance between 50-200 | ||
− | # Create | + | # Create 10 turtles and place them 15 units apart from each other vertically |
+ | # After successfully putting 10 turtles 15 units apart vertically, use a loop that runs 360 times to tell each one to draw a small arc. | ||
+ | #* HINT: You'll need a loop inside of a loop (2 loops!) | ||
+ | #* HINT: turtles[x].circle(x * 15, 1) |
Latest revision as of 10:04, 6 February 2012
Objectives:
- You will practice creating lists of turtles
- You will integrate loops and lists into drawing graphics with Python's turtle
Resources:
Directions:
- Use the following template in a file named listTurtle.py:
from turtle import *
#speed(0) # fastest turtle
#create empty list of turtles
turtles = []
#populate the list of turtles with 10 turtles
x = 0
while x < 10:
turtles.append(Turtle())
x = x + 1
#do stuff with turtles
#you'll need to use the loop
x = 0
while x < 10:
turtles[x].forward(x * 100)
x = x + 1
exitonclick()
- What do you see when you run the code above?
- Let's try to create some other graphics with Turtle, lists, and loops. Complete the following drawings, but you must create new files for each (e.g. listTurtle1.py,listTurtle2.py, etc.)
- Tell each turtle to draw a circle using its element/index number
- Tell each turtle to go to a random location and draw a random circle
- Tell each turtle to go to a random location and move forward a random distance between 50-200
- Create 10 turtles and place them 15 units apart from each other vertically
- After successfully putting 10 turtles 15 units apart vertically, use a loop that runs 360 times to tell each one to draw a small arc.
- HINT: You'll need a loop inside of a loop (2 loops!)
- HINT: turtles[x].circle(x * 15, 1)