left
Turn anticlockwise by this many degrees.
left(90)forward(60)
left(90)
forward(60)
Objects with turtles · In any playground, open Reference to copy these.
Turn anticlockwise by this many degrees.
left(90)forward(60)
left(90)
forward(60)
Lift the pen so moving does not draw.
penup()forward(40)
penup()
forward(40)
pendown()
forward(40)
Repeat the indented lines this many times.
for i in range(4):
passfor i in range(5):
forward(15)
penup()
forward(10)
pendown()
i % 2 == 0 is true on every second pass.
for i in range(8):
if i % 2 == 0:
pass
else:
passfor i in range(8):
if i % 2 == 0:
pencolor("red")
else:
pencolor("blue")
pensize(6)
forward(30)
right(45)
Use the counter i to make each pass different.
for i in range(20):
forward(i * 5)
right(90)for i in range(30):
forward(i * 5)
right(90)
Keep many values in order under one name. The first item is place 0.
colours = ["red", "blue", "green"]Work through a list, one item at a time.
for c in colours:
passpensize(6)
for c in ["red", "blue", "green"]:
pencolor(c)
forward(40)
Show text or a value in the output box.
print("Hello")Pick a random whole number between two values.
import random
n = random.randint(1, 10)Run a function every time a key is pressed. Click the picture first so it receives keys.
def go_up():
forward(20)
onkey(go_up, "Up")
listen()
mainloop()def go_up():
forward(20)
go_up()
go_up()
go_up()
Turn a number into text, so it can be joined: "Score: " + str(score).
str(score)Make another turtle with its own name. Put the name and a dot in front of a command to give it to that turtle.
star = Turtle()
star.shape("circle")
star.forward(50)True if the item is in the list (or the key is in the dictionary, or the letters are in the text).
if item in bag:
passJump to a position. (0, 0) is the centre; x goes right, y goes up.
goto(0, 0)goto(80, 60)
How fast the turtle moves, from 1 (slow) to 10 (fast).
speed(10)Draw a square by repeating forward and right four times.
for i in range(4):
forward(100)
right(90)
Start the total at 0 once, then add to it on every pass.
total = 0
while total < 10:
total += int(input("Add: "))Do one thing or the other, depending on a condition.
if x > 0:
pass
else:
passfor x in [-90, -30, 30, 90]:
penup()
goto(x, 0)
if x > 0:
dot(30, "red")
else:
dot(30, "blue")
hideturtle()
Give a value a name, so you can reuse and change it.
size = 50length = 120
forward(length)
left(90)
forward(length / 2)
Change a value every pass: read the old one, store a new one.
size = 5
for i in range(20):
forward(size)
right(90)
size = size + 5size = 5
for i in range(30):
forward(size)
right(90)
size = size + 5
Name a group of steps, then run it by calling its name.
def shape():
pass
shape()def zigzag():
left(45)
forward(30)
right(90)
forward(30)
left(45)
zigzag()
zigzag()
zigzag()
Run a function again after a number of milliseconds. A function can start its own timer.
def tick():
forward(5)
ontimer(tick, 50)
tick()
mainloop()Join pieces of text into one with +. Spaces only appear where they are inside the quotes.
"Hello, " + nameAdd to a variable: total += 5 is short for total = total + 5.
total += 1Values stored under names (keys). One dictionary can hold everything about one thing.
star = {"speed": 5, "points": 1}The value stored under that key. The key goes in quotes. Assign to it to change it: star["speed"] = 8.
star["speed"]A new kind of object. __init__ runs when one is made; self.name = value stores a value in that object.
class Star:
def __init__(self, x):
self.speed = 5
s = Star(100)A function inside a class. Call it on one object: star.move(). self is that object.
def move(self):
self.turtle.forward(self.speed)