Forward n
- Moves forward by n meters and draws a line on the groundLeft d
- turns left by d degreesForward 10 Left 90 Forward 10 Left 90 Forward 10 Left 90 Forward 10 Left 90You can argue that the last
Left 90
is not necessary and you would
probably be correct. The above example points out two things: One, that we need
to know what are the basic commands are we are able to execute (in this case Forward and Left) and
two, we need to put those commands in the right sequence to achieve our final
results.
from turtle import * # this line allows us to use the Turtle library
forward(100)
left(90)
forward(100)
left(90)
forward(100)
left(90)
forward(100)
left(90)
forward(100)
and left(90)
) four times to draw the square. This
repetition is a common occurrence in programming so there is an easy construct
for this in the Python language.
from turtle import * # this line allows us to use the Turtle library
for i in range(4):
forward(100)
left(90)
for i in range(4)
instruction
are executed four times in sequence.
n
instructions that we need repeated xx
number of times, we can write the
following code:
for i in range(x)
statement 1
statement 2
statement 3
.....
statement n
from turtle import *
forward(100)
left(90)
forward(100)
left(90)
forward(100)
left(90)
forward(100)
left(90)
from turtle import *
for i in range(4):
forward(100)
left(90)
drawSquare
, and then be able to use it from other places in
my code.
drawSquare
function:
def drawSquare():
for i in range(4):
forward(100)
left(90)
drawSquare()
from turtle import *
def drawSquare():
for i in range(4):
forward(100)
left(90)
drawSquare()
from turtle import *
def drawSquare():
for i in range(4):
forward(100)
left(90)
drawSquare()
forward(200)
drawSquare()
forward(200)
drawSquare()
forward(200)
Command | Functionality |
---|---|
from turtle import * |
Make the turtle module/package available for use |
forward(distance) |
Moves the turtle forward distance, drawing a line behind the turtle |
backward(distance) |
Moves the turtle backward distance, drawing a line behind the turtle |
right(angle) |
Turns the turtle right by angle degrees |
left(angle) |
Turns the turtle left by angle degrees |
penup() |
Stop all drawing until pendown is called |
pendown() |
Resume drawing after a call to penup() |
color(color) |
Change the turtle's current color |
done() |
Stop drawing with turtle |