INTRODUCTION |
You have already gotten to know the command repeat, with which you can repeat a program block several times. However, it is important to know that you can only use repeat this way in TigerJython and not in other Python flavours. On the other hand, you can use the while structure everywhere. |
SPIDER WEB |
from gturtle import * makeTurtle("sprites/spider.png") a = 5 while a < 200: forward(a) right(90) a = a + 2
|
MEMO |
A while loop is used to repeat a program block. In order for the program block to be executed, the condition must be true. Because of this, one might also call it a "running condition". If the change in value is missing in the loop block, the running condition always stays true and the program remains endlessly "hanging" in the loop. In our learning environment, you can cancel the hanging program with the stop button or by closing the turtle window. In general, infinite loops without a the option to cancel are dangerous, and in an extreme case you will have to reboot your computer. |
COMBINING CONDITIONS WITH OR |
from gturtle import * def triangle(): repeat 3: forward(100) right(120) makeTurtle() i = 0 while i < 6: if i == 0 or i == 2 or i == 4: setPenColor("red") else: setPenColor("green") fillToPoint(0, 0) triangle() right(60) i = i + 1 |
MEMO |
You must pay attention to the correct indentation for each loop block when using several program structures. As you can see, you can combine two or more conditions using or. A condition linked this way is true if either of the conditions are satisfied, and it is also true if both conditions are met. Using the command fillToPoint(x, y) you can fill figures with the pen color while drawing, as opposed to the command fill(), with which you can fill already drawn closed figures. |
COMBINING CONDITIONS WITH AND |
from gturtle import * makeTurtle() setPos(-200, 30) right(30) fillToHorizontal(0) setPenColor("sienna") nr = 1 while nr <= 10: if nr > 3 and nr < 8: forward(60) right(120) forward(60) left(120) else: forward(30) right(120) forward(30) left(120) nr += 1 |
MEMO |
You can link two conditions with and. Such a linked statement is only true if both conditions are met. Using the command fillToHorizontal(y) you can fill figures with the pen color while drawing. This way, the area between the drawn figure and the horizontal line at y is filled.nr += 1 means that nr is increased by 1. It is just an abbreviation for the assignment nr = nr + 1. |
EXITING LOOPS WITH BREAK |
from gturtle import * def square(sidelength): repeat 4: forward(sidelength) left(90) makeTurtle() hideTurtle() i = 0 while True: if i > 120: break square(i) right(6) i += 2 print("i =", i) |
MEMO |
With a while True loop, a program block can be repeated, until the program is aborted by closing the turtle window. The loop is run in increments of two. Instead of using i = i + 2 you should use the abbreviated notion i += 2 (i is incremented by 2).With the command print-you can write something into the TigerJython console at the bottom of the editor. With text you should use quotation marks and numbers should be separated by a comma. A space will automatically be inserted between the text and the number. Do you understand why the output is i = 122? The keyword continue is rarely used. It skips the remaining part of the body of the loop. |
INPUT VALIDATION |
If you ask the user to enter a number restricted to a certain range, you cannot trust him that he adheres to your restriction. A "robust" program checks the input and intercepts an incorrect entry with a feedback. This input validation is most easily performed in a while loop which is repeated until the input value is accepted. In your program the user enters the number 1, 2, or 3 to select one the colors red, green, or yellow of the filled circle. from gturtle import * makeTurtle() n = 0 while n < 1 or n > 3: n = inputInt("Enter 1, 2 or 3") if n == 1: setPenColor("red") elif n == 2: setPenColor("green") else: setPenColor("yellow") dot(200) |
EXERCISES |
|