40 likes | 241 Views
We start by importing the turtle command library from turtle import*. This programming loop uses the FOR instruction. This works like the REPEAT command. The i is a variable which is used to count the steps in the loop don’t confuse i with a 1
E N D
We start by importing the turtle command library from turtle import* This programming loop uses the FOR instruction. This works like the REPEAT command. The iis a variable which is used to count the steps in the loop don’t confuseiwith a 1 For i in range (4)Is the same as saying Repeat 4 Notice that the repeat 5 [rt72 fd 100] We use the TAB key to indent from turtle import* for i in range(5): rt(72) fd(100) 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
print ("This program draws shapes based on the number you enter in a uniform pattern.") num_str = input("Enter the side number of the shape you want to draw: ") if num_str.isdigit(): squares = int(num_str) 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() turtle.end_fill() turtle.begin_fill() # Begin the fill process. turtle.down() # "Pen" down? for i in range(squares): # For each edge of the shape turtle.forward(40) # Move forward 40 units turtle.left(angle) # Turn ready for the next edge turtle.up() # Pen up turtle.end_fill() # End fill.