HomeTurtlegraficsGPanelRobotics WebTigerPython
 Python - Online
Deutsch   English   

3. Repeat / random number

 

 

YOU LEARN HERE...

 

that you can execute certain program parts several times with the help of the repeat loop repeat.

Random numbers form the basis of many simulations. They are generated using a random algorithm, which is contained in thel random module in Python.

 

 

EXAMPLES

 


Example 1
: Flashing light
Similar to a flashing light, a red circle is to be displayed and extinguished 10 times in succession. You first draw a red filled circle and leave it displayed for 400 milliseconds. You can stop a running program with thel delay(time) command. Then paint over the circle with a white circle and stop the program for 400 milliseconds. To repeat this process 10 times, use the command repeat 10: (the colon is important). The instructions that are repeated form a loop block and must all be equally spaced. You use four spaces or the tab key to do this.

Program:      

# Gp3a.py
from gpanel import *

makeGPanel(-10, 10, -10, 10)

repeat 10:
    setColor("red")
    fillCircle(3)
    delay(400)
    setColor("white")
    fillCircle(3.1)
    delay(400)
► Copy to clipboard
 


Example 2
: Drawing circles at random positions

The randint(a, b) command returns a random integer number in the range a to b (including both limits). For example, random dice numbers are obtained with randint(1, 6). The random() function generates random numbers between 0 (included) and 1 (excluded) with 10 decimal places (e.g. 0.69880747453)(ausgeschlossen) mit 10 Dezimalstellen erzeugt (z.B. 0.69880747453)

In the example, 100 small red circles are to be randomly distributed over the area of the graphics window. First, the position of the circle is defined with two random numbers in the range 0 to 100 and a circle is drawn. With repeat 100: this process is repeated 100 times.

Program:      

# Gp3b.py
from gpanel import *
from random import randint

makeGPanel(-2, 102, -2, 102)

setColor("red")
repeat 200:
    pos(randint(0, 100), randint(0, 100))
    fillCircle(2)
    delay(50)
► Copy to clipboard
 

 

 

REMEMBER YOU...

 

You can use repeat() to execute program blocks several times. Python also has while and for loops, but these require the concept of variables and will therefore be introduced later.

from random import randint imports the randint(a, b) function from the random module. This returns an integer random number in the range a to b (including both limits).

 

 

TO SOLVE BY YOURSELF

 

1)


Program a flashing system that changes the colors red, yellow and green one after the other and repeats this process 10 times. Choose a display period of 400 ms. If you omit the number of repetitions in the repeat command, the system flashes until the graphics window is closed.

 
 

2)

Draw connecting lines from point (0, 0) to 200 randomly selected points.
 


 

3)

In the adjacent “work of art”, the position and radius of the filled circles and also the colors are randomly selected and result in a completely different image each time the program is executed.

To create the colors, use the command
setColor(r, g, b), where the numbers r, g, b are the red, green and blue components respectively (a number between 0 and 255).
Random colors are obtained with:
setColor(randint(0, 255), randint(0, 255), randint(0, 255)


Zufällige Farben erhält man mit:
setColor(randint(0, 255), randint(0, 255), randint(0, 255))