HomeTurtlegraficsGPanelRobotics WebTigerPython
 Python - Online
while&for
Deutsch   English   

10. WHILE & FOR

 

 

YOU LEARN HERE...

 

how to use repeating structures with the keywords while and for. The while loop is one of the most important program structures of all. It can generally be used for any type of repetition and is found in practically all programming languages. With repeat, you could previously program simple repetitions without using variables. Now that you know the concept of variables, you can also use the while and for structure.

 

 

EXAMLES FOR WHILE-LOOP

 

A while loop is introduced with the keyword while followed by a condition and a colon. As long as the condition is fulfilled, the commands are repeated in the subsequent program block. The comparison operators >, >=, <, <=, ==, != are usually used in the condition. The instructions in the while block must be indented.

The repetition structure can be formulated colloquially as follows: “As long as the following condition is met, execute the following program block...”


While loops are interesting when the number of runs is not fixed from the outset. In your example, the longest section of the spiral should be less than 200.

You start with the distance a = 5. As long as a < 200, you increase the distance by 2 after each rotation.

 

 

Programm:    

from gturtle import *

a = 5
while a < 200:
    forward(a) 
    right(90)
    a = a + 2
► In Zwischenablage kopieren


 

Endless while loo
The Turtle moves “endlessly” on the circle.
The keyword while True: initiate a so-called endless while loop.

The commands in the loop block are repeated until you end the program by clicking on the red Stop button.

 

Program:    

from gturtle import *

while True:
    forward(3)
    right(3)
► In Zwischenablage kopieren

 

 

REMEMBER...

 

The condition a < 200 is also called a run condition.

While True initiates an endless loop in which the commands in the loop block are repeated until Stop is pressed.

 

 

TO SOLVE BY YOURSELF


  1.

The Turtle draws regular triangles, where the first triangle has the side length s = 300, the second 290, the third 280 and so on. Each triangle is followed by a rotation of 10° to the right. It stops drawing when the side length is less than 10.

 

  2.

The turtle moves endlessly back and forth in small steps in the range x = -250 to 250.

Use a condition with the function getX(), which returns the current x-coordinate.

 

 

   
 

 

EXAMPLES FOR FOR-LOOP

  With the for-loop, the loop counter is changed automatically.

The turtle moves forwards by a distance s and turns 70° to the right. s is increased by 1 after each pass. The loop counter is used to determine the length of the distance.

With for s in range(100): the numbers 0, 1, 2,..., 99 are run through, so the loop block is repeated 100 times.

 

Program:    

from gturtle import *

hideTurtle()

for s in range(100):
    forward(s)
    right(70)
Copy to clipboard
You can also draw the next figure with two repeat loops. Since you also need i in the inner loop to calculate the line length, the for loop is more advantageous. Pay attention to the correct indentation!

Program:    

from gturtle import *

setPenColor("blue")

repeat 5:
    for i in range(12):
        forward(12 * i)
        left(144)
hideTurtle()
► Copy to clipboard
 

The general form of a for loop has 3 parameters
for i in range(start, stop, step):
The first parameter specifies the start value of the variable i. As long as i is smaller than stop, the loop is repeated, increasing i by step at each pass. Here you use a for loop to determine the coordinates of the points.

Program:    

from gturtle import *

for x in range(-240, 250 , 20):    
    setPos(x, 0)
    dot(15)
► Copy to clipboard
 

 

 

REMEMBER...

 

With for i in range(n):the numbers 0, 1, 2,...(n-1) are run through. The parameter n in range(n) specifies the number of repetitions.

A for loop can also have 2 or 3 parameters.
for i in range(5, 20): runs through the numbers 5, 6, 7, 8.....19. (step = 1).
for i in range(5, 20, 3): runs through the numbers 5, 8, 11, 14, 17.

 

 

TO SOLVE BY YOURSELF


  3.

You create the adjacent figure by using a for loop to draw 20 hexagons with increasingly larger side lengths.

def hexagon(i):
    repeat 6:
        forward(i)
        right(60)
 
  4.

The turtle moves forwards drawing a filled circle (dot), then backwards to the starting point and turns 10° to the right. With each repetition it draws a longer distance.

Solve the task with a for loop and use a multiple of the loop counter i for the distance.

 

  5.

Create the adjacent figure with a for loop. The turtle starts with the path length s forwards and then turns 89° to the right. It should run from 0 to 150.

 

 

 

ADDITIVEL: NESTED FOR LOOPS

 


The for loops can be easily “nested”. In this example, the for loop with y coordinates is run through for each coordinate x. As ther step in both loops is 20, x and y are increased by 20 with each loop run. At stop, select 201 so that the coordinate x = 200 or y = 200 is also taken into account.

 


Program:    

from gturtle import *

for x in range(-200, 201, 20):
    for y in range(-200, 201, 20):   
        if x + y < 0:
            setPenColor("red")
        else:
            setPenColor("green")
        setPos(x, y)
        dot(10) 
► Copy to clipboard

 


Use this method to draw a chessboard. You can easily see that you must always draw a filled square if the sum of the row and column index is an even number.

Since each square has the size 30x30, the square (i, k) is drawn at the position x = 30 * i, y = 30 * k.

 

Program:    

from gturtle import *

def cell(x, y):
    setPos(x, y)
    startPath()
    repeat 4:
        forward(30)
        left(90)
    fillPath()    

setFillColor("blue")
hideTurtle()
for i in range(8):
    for k in range(8):
        if (i + k) % 2 == 0:
            cell(30 * i, 30 * k)
► Copy to clipboard