INTRODUCTION |
What you do in your daily life often depends on certain conditions. So let's say you decide on how you will get to school today depending on the weather. You say: "In case it rains today, I'll take the tram, otherwise I'll ll ride my bike". Similar to this, flow of a program can also depend on certain conditions. Among the basic structures of any programming language are such program branches that depend on specific conditions. The instructions after if are only executed when the condition is true, otherwise the statements after else are executed. |
REVIEWING INPUTS |
from gturtle import * def square(sidelength): repeat 4: forward(sidelength) right(90) makeTurtle() s = inputInt("Enter the side length") if s < 300: square(s) else: print("The side length is too big") |
MEMO |
The instructions after if are only executed if the condition is true, otherwise the statements after else are executed. You can also leave out the else block. Try it! Please be aware of the colons after the if condition and after else, as well as a correct indentation of both program blocks. |
MULTIPLE SELECTION |
from gturtle import * def square(): repeat 4: forward(100) right(90) makeTurtle() n = inputInt("Enter a number: 1:red 2:green 3:yellow") if n == 1: setFillColor("red") elif n == 2: setFillColor("green") elif n == 3: setFillColor("yellow") else: setFillColor("black") square() fill(10, 10)
|
MEMO |
Several conditions can be checked consecutively. In a case where the condition at if is not fulfilled, the condition at elif is checked. elif is an abbreviation of else if. In a case where none of the elif conditions are fulfilled, any statements after else are executed. |
COLOR CHOICE, BOOLEAN VARIABLES |
The star that you draw uses the function star(), which in addition to the size of the star also has a parameter filled whose value can be true or false and determines whether the star should be filled or not. from gturtle import * makeTurtle() def star(size, filled): if filled: startPath() repeat 9: forward(size) left(175) forward(size) left(225) if filled: fillPath() clear("black") repeat 5: color = askColor("Color selection", "yellow") if color == None: break setPenColor(color) setFillColor(color) setRandomPos(400, 400) back(100) star(100, True)
|
MEMO |
The function askColor() has parameters for the text in the title bar and the color that is selected as the default value. When you click the OK button the function returns the selected color and when you click the cancel button instead, the function returns the special value none. You can test this value with an if statement and you can abort the repeat loop with break. A variable or a parameter which can take the values true or false is called a boolean variable or a boolean parameter [more... The name comes from the mathematician George Boole, who invented the logical calculus]. You can directly test its value, for instance with if filled: It is thus not necessary (and not very elegant) to write if filled == True. |
EXERCISES |
|