deutsch     english    français     Print

 

3. REPETITION

 

 

INTRODUCTION

 

Computers are particularly good at repeating the same instructions (including turtle commands) over and over again. In order to draw a square, you do not need to enter the commands forward(100) and left(90) four times in a row. It is rather sufficient to tell the turtle to simply repeat the two statements four times.

With the instruction repeat, you tell the turtle that some commands should repeat a designated number of times. In order for the computer to know that these commands belong together (forming a program block), they must be equally indented. Typically, we use four spaces for indentation.

PROGRAMMING CONCEPTS: Simple repeat loop, repeat loop instead of code duplication

 

 

REPEAT - STRUCTURE

 

In order to draw a square, the turtle has to move straight ahead four times and make a total of four 90° turns. If you were to write out each command separately, the program would become quite long.

With the instruction repeat 4: you tell the turtle to repeat the indented lines four times. Make sure not to forget the colon!


 

from gturtle import *

makeTurtle()
repeat 4:
    forward(100) 
    left(90)
Highlight program code (Ctrl+C copy, Ctrl+V paste)

 

 

MEMO

 

With repeat n: you tell the computer that it should repeat one or more instructions n times before it executes further instructions. Everything that is to be repeated must be placed below repeat, and must also be indented.

repeat number:
    Instructions, that
    should be
    repeated

 

 

REPEATING SOUNDS

 

A typical example of a repetition is the Dah-Dih-Dah-Dih sound of fire trucks. Using the turtle you can easily create such a tone sequence, and simultaneousely for fun, you can let the turtle draw a zigzag curve. You can generate a pure tone with playTone(), where you specify its pitch as a frequency (in Hertz) and its duration (in milliseconds).


 

from gturtle import *

makeTurtle()

setPos(-200, 0)
right(45)

repeat 5:
    playTone(392, 400)   
    forward(50)    
    right(90)
    playTone(523, 400)
    forward(50)  
    left(90)
Highlight program code (Ctrl+C copy, Ctrl+V paste)

 

 

MEMO

 

With setPos(x, y) you can directly put the turtle into a designated position in the window without actually making a trace. The two numbers, x and y, are the coordinates relative to the zero point, which is located at the middle of the window. (The coordinate range depends on the size of the window.)

You can also specify the pitch of playTone() using a letter according to the musical scale, for example with c, d, etc., or in the one-line octave with c', d', etc. (or with two or three apostrophes). You have to put quotation marks around the pitch. If you want, you can also indicate a musical instrument to be used (available are: piano, guitar, harp, trumpet, xylophone, organ, violin, panflute, bird, seashore). Try it once with:

Lower Ton: playTone("g'", 400, instrument = "trumpet")
Higher Ton: playTone("c''", 400, instrument = "trumpet")

 

 

NESTED REPEATS

 

A square can be made quite easily with a four-fold repetition. Now let's draw 20 squares, with the squares slightly rotating against each other. You first have to nest the two repeat statements into each other. In the inner program block, the turtle draws a square and then turns by 18 degrees to the right. The outer repeat statement repeats this 20 times. Please make sure to correctly indent the statements that should be repeated.

If you hide the turtle with hideTurtle(), it will finish drawing quicker.

 

 
from gturtle import *

makeTurtle()

# hideTurtle()
repeat 20:
    repeat 4:
        forward(80) 
        left(90)
    right(18)      
Highlight program code (Ctrl+C copy, Ctrl+V paste)

 

 

MEMO

 
The repeat commands can be nested. It is very important to have the correct indentations in the statements which are to be repeated.

 

 

EXERCISES


  1. Draw a staircase with seven steps.  

  2. Draw a star using the back() command.  

  3. You can draw a “real” star with rotation angles 140° and 80°.  

  4. Draw the following figure using two nested repeat statements. The inner repeat block will draw a square.  

  5.

Draw a pearl necklace.

 

 

  6. Draw a bird.