For loops - kingsmeadspecialisms.com

advertisement
We start by importing the turtle
command library
This programming loop uses the
FOR instruction. This works like the REPEAT command.
from turtle import*
The
i
is a variable which is used to count the steps in the loop
don’t confuse i with a 1
from turtle import*
for i in range(5):
rt(72)
fd(100)
For i in range (4) Is the same as saying Repeat 4
Notice that the repeat 5 [rt 72 fd 100]
We use the TAB key to indent
We then start programming the
turtle using the
Command instructions and
parameters
Lets look at a blockly example to compare loops
a= 0
for i in range(5):
a=a+1
print a
from turtle import*
import turtle
import time
import random
turtle.up
turtle.begin_fill()
pencolor('blue')
turtle.color(random.random(),random.random(),
random.random())
i=0
while (i<5):
rt(72)
fd(100)
i=i+1
turtle.end_fill()
time.sleep(2)
turtle.bye()
from turtle import *
color('red', 'yellow')
begin_fill()
while True:
forward(200)
left(170)
if abs(pos()) < 1:
break
end_fill()
done()
Use this for help sheet.
http://www.eg.bucknell.edu/~hyde/Python3/TurtleDirections.html
angle = 180 - 180*(squares-2)/squares
turtle.up
x=0
y=0
turtle.setpos(x,y)
numshapes = 8
for x in range(numshapes):
turtle.color(random.random(),random.random(),
random.random())
x += 5
y += 5
turtle.forward(x)
turtle.left(y)
for i in range(squares):
turtle.begin_fill()
turtle.down()
turtle.forward(40)
turtle.left(angle)
turtle.forward(40)
print (turtle.pos())
turtle.up()
Download