INTRODUCTION |
The concept of variables is very important to programming. Therefore, you need to give a special effort in order to understand it as thoroughly as possible. You already know that a variable is a memory slot that is addressed with a name and that holds a value. You also know that parameters can be understood as “volatile” memory slots which, when their function is called, receive a value that the function can then access during its execution. |
A MOSAIC OF 10X10 STONES |
from gpanel import * makeGPanel(0, 400, 0, 400) for y in range(0, 400, 10): for x in range(0, 400, 10): setColor(getRandomX11Color()) move(x + 5, y + 5) fillRectangle(10, 10) delay(1) |
MEMO |
Whenever a grid needs to be run through, two nested for loops are best suited. Think about why precisely the stones are placed line by line from bottom to top. You need a shift at move() because the stones are anchored/established in the middle. The method getRandomX11Color() gives back (as a word) one of the colors from the X11 color palette, which you can then pass on to setColor(). You can first call the function getRandomX11Color(), and then the function setColor(). |
MAGIC NUMBERS |
Now you have to go back through the code line by line in order to find the error. If this is necessary to adapt the program to a new situation (reuse), then your program was correct but just poorly written. You should get into the habit of having a good programming style, so that you can easily adjust programs to (fit) new situations. How can you proceed? Instead of writing the stone size as a fixed number in the code, define a variable size, that can be used in place of the fixed number wherever the stone size plays a role. In order to structure the program even better, you should also write a separate function drawStone() for the placement of the stone. from gpanel import * def drawStone(x, y): setColor(getRandomX11Color()) move(x + size/2, y + size/2) fillRectangle(size, size) makeGPanel(0, 400, 0, 400) size = 50 for x in range(0, 400, size): for y in range(0, 400, size): drawStone(x, y) |
MEMO |
The use of fixed numerical values throughout the program results in poorly reusable programs. You should instead define variables and use them in place of numbers. To indicate that these should never change, sometimes you write these variables in capital letters and call them constants. A variable that is defined in the main block can also be read in each function. Therefore, we also call it a global variable. For longer self-contained actions, the code should be put into separate functions. This has several advantages: the first is that you can recognize the function name and what it should do, the second is that you can call them several times without having to rewrite the code, and the third is that the program becomes clear and comprehensible. This type of programming is called structured programming (or procedural programming) and it is an important trait of a good programming style. |
EXERCISES |
|