Fors and Turtles! ================
For this exercise, you will use a single turtle in more complex situations:
- In a for loop using
range
- In a for loop using
range
andif
to change the colors - Fizz Buzz! Read below for instructions =).
Basic turtle setup:
### first, import turtles
import turtle
### then, create a turtle!
bob = turtle.Turtle()
bob.speed('fastest')
### do stuff with the turtle
# for example, a triangle!
for i in range(3):
bob.forward(100)
bob.left(120)
### end the code with the following, so python knows to keep the window open
turtle.done()
import turtle
bob = turtle.Turtle()
for i in range(500):
bob.forward(i)
bob.left(95)
Change the code so that each time around the for
loop, the turtle draws a triangle or a square.
import turtle
bob = turtle.Turtle()
color1 = "#cc3333" ### you can use the internet to get more of these
color2 = "#3333cc"
for i in range(500):
bob.forward(i)
bob.left(95)
if False:
print("change colors here")
Change the code above so that the if
statement will change the turtle's color when i
is even.
Fizz Buzz!
Modify the code in Exercise 2 so that:
- when
i
is a multiple of 3
- turn the turtle color1
- make the turtle write "fizz"
- when
i
is a multiple of 5
- turn the turtle color2
- make the turtle write "buzz"
- when
i
is a multiple of both
- turn the turtle color3
- make the turtle write "fizz buzz"
- else, make the turtle black and write nothing.
You can "nest" loops inside each other:
for i in range(5):
for j in range(2):
print(i,j)
Use a nested loop inside the turtle's for loop to do more interesting patterns.
Lists let you store ordered sets of things. There are a couple different ways to make a list.
mylist = list()
mylist.append(5)
mylist.append(10)
print(mylist)
print(mylist[0])
print(mylist[1:])
mylist = [5, 10]
print(mylist)
print(mylist[0])
print(mylist[1:])
You can use lists in for loops instead of range
for item in mylist:
print(item)
Do the following: 1. create a list with multiple turtles :: turtle_list = [turtle.Turtle()] turtle_list.append(turtle.Turtle()) 2. use a second for loop inside the main for loop to make each turtle move :: for t in turtle_list: # code here