Skip to content

Latest commit

 

History

History
151 lines (100 loc) · 3.06 KB

for_turtles.rst

File metadata and controls

151 lines (100 loc) · 3.06 KB

Fors and Turtles! ================

Link to turtle cheatsheet

For this exercise, you will use a single turtle in more complex situations:

  1. In a for loop using range
  2. In a for loop using range and if to change the colors
  3. 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()

Exercise 1

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.

Exercise 2

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.

Exercise 3

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.

Bonus Exercise 1

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.

Exercises that use a list

Exercise 1

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