Deutsch English |
YOU LEARN HERE... |
how to proceed so that program blocks are only executed under certain conditions. You will also learn how to negate conditions and how to combine them with AND and OR operators. |
EXAMPLES |
In your program, use the randint(-250, 250) command to generate two random numbers x and y and let the turtle jump to the position (x, y). Draw a red dot there if the turtle is inside the circle with a radius of 250, otherwise draw a gray dot. Repeat this process 10,000 times. Sometimes this is also referred to as random rain. If the turtle is inside the circle, Pythagoras' theorem states that its distance squared rsquare to the origin is less than 250 * 250 = 62500.
Program: from gturtle import * from random import randint makeTurtle() hideTurtle() repeat 10000: x = randint(-250, 250) y = randint(-250, 250) setPos(x, y) rsquare = x * x + y * y if rsquare < 62500: setPenColor("red") dot(4) else: setPenColor("gray") dot(4) |
REMEMBER... |
Instead of saying that a condition is fulfilled or not fulfilled, you can also say that the condition is true or false. If you don't want the gray dots to be drawn, you can also leave out the else part. Try it! If you want to cancel the program prematurely, click on the red square button. |
LINK CONDITIONS WITH AND |
Program: from gturtle import * from random import randint makeTurtle() hideTurtle() repeat 10000: x = randint(-250,250) y = randint(-250,250) setPos(x, y) if x > -100 and x < 100: setPenColor("red") dot(4) else: setPenColor("gray") dot(4) |
LINK CONDITIONS WITH OR |
Program: from gturtle import * from random import randint makeTurtle() hideTurtle() repeat 10000: x = randint(-250,250) y = randint(-250,250) setPos(x, y) if (x > -50 and x < 50) or (y > -50 and y < 50): setPenColor("red") dot(4) else: setPenColor("gray") dot(4) |
NEGATE CONDITION |
Program: from gturtle import * from random import randint makeTurtle() hideTurtle() repeat10000: x = randint(-250, 250) y = randint(-250, 250) setPos(x, y) if not (x > -100 and x < 100): setPenColor("red") dot(4) else: setPenColor("gray") dot(4) |
REMEMBER... |
The correct use of parentheses in conditions is very important. Since not binds more strongly than and and or, you have to put a bracket here. The weakest binding is or. Logically, you also get the gray stripe if you require that x <= -50 or x >= 50. So you can formulate conditions in different ways. Give it a try! The following comparison operators are available for numbers:
In particular, you need to get used to the doubling of the equals sign in the equality condition. This is necessary so that the computer can distinguish between the assignment and the equality condition. |
MULTIPLE SELECTION |
To distinguish between more than two cases, you have to insert another if condition in the else part. In Python, you can use the shorter elif and write:
Program: from gturtle import * from random import randint makeTurtle() hideTurtle() n = randint(1, 6) if n == 1: setPenColor("red") elif n == 2: setPenColor("yellow") elif n == 3: setPenColor("magenta") elif n == 4: setPenColor("green") elif n == 5: setPenColor("blue") else: setPenColor("black") dot(100) |
TO SOLVE BY YOURSELF |
|
|