Values and variables#
A value is something that is stored in the computer and can be manipulated by a computer program. In the following, values are referred to as objects
and the words are used synonymously.
Bemerkung
In other programming languages, a distinction is made between primitive data types, which can only be modified, and objects, which often also bring attributes and methods, such as the class world
, which has the attribute size
and whose background can be used with the method add_background
.
Python hat die einfache Philosophie: Alles ist ein Objekt - Daher wird hier generell der Begriff Wert verwendet.
Every value has a data type, which you can query, for example, the following program provides:
from miniworlds import *
import random
world = World((100,100))
print(type("Hello World"))
print(type(Line((10,10), (100,100))))
print(type(17))
world.run()
The following output on the command line:
<class 'str'>
<class 'actors.shapes.Line'>
<class 'int'>
Variablen#
In order to find objects created on the computer again, one must save where they can be found. On a technical level, the memory location of an object in the computer is stored for this purpose. In a programming language like Python, we use a name to find objects again.
line = Line((10,10), (100,100))
Stores the line under the name line. If you now use the name line, you can access the variable and modify the object.
For example, in the previous chapters, you have already changed the color of a line:
line.fill_color = (255,0,0)
In gleicher Weise kannst du z.B. auch mit Zahlen rechnen
a = 3
b = 4
print(a + b)
Assignments#
Assignments are written as:
c = a + b
Das bedeutet:
First, the result on the right side is evaluated.
Then the result is stored in the variable on the left side.
According to the following program:
a = 3
b = 4
c = a + b
a hat den Wert 3, b den Wert 4 und c den Wert 7.
In gleicher Weise kannst du aber auch Werte von Objekten, z.B. die Position eines Kreises, verändern. Das folgende Programm lässt dich einen Kreis mit den Tasten a und d nach links oder rechts bewegen. Die x-Position ist über den Namen circle.x
zugreifbar und kann auch so verändert werden.
from miniworlds import *
import random
world = World((100,100))
circle = Circle((50,50), 20)
@world.register
def on_key_pressed_a(self):
circle.x = circle.x - 1
@world.register
def on_key_pressed_d(self):
circle.x = circle.x + 1
world.run()
The line circle.x = circle.x + 1
says the following: First calculate the value circle.x + 1
(i.e., increase the x-coordinate of the circle by 1) and store the result of this calculation back in circle.x
.
Bemerkung
Das = bedeutet nicht, dass der Ausdruck links und rechts mathematisch gleich ist. Stattdessen wird das Ergebnis der rechten Seite zuerst ausgewertet und das Ergebnis dieser Berechnung in die Variable auf der linken Seite gespeichert.
One reads the expression a = b
as b is assigned to a.
Some programming languages use a different symbol instead of the = sign to avoid confusion among beginner programmers.
Verwendung#
Everywhere you have used a number or text so far, you can also directly use variables, e.g.
a = 3
b = 4
line = Line((a, b), (5, 6))
This works whenever the data type of the variable matches the expected data type.
Der folgende Code z.B. führt z.B. zu einem Fehler:
a = 3
b = 4
line = Line(a, (5, 6))
Line erwartet ein Tupel und erhält aber nur eine Integer-Variable. Daher wird folgender Fehler ausgegeben
miniworlds.exceptions.miniworlds_exception.ActorArgumentShouldBeTuple: First argument to create a Actor [position] should be a Tuple.
The error is trying to give you a hint about what you did wrong, so it often helps to read the error messages.
Gültigkeitsbereich - Umfang#
When programmers write larger programs - often in a team - the names of variables take on special significance: How can one prevent other programmers from using their own variable names and potentially causing unforeseen side effects?
The answer to this is „Scopes: A variable has different scopes depending on where it was defined:
A variable introduced within a function has a local scope. It is locally visible within this function, but not within other functions.
A variable defined outside a function is globally visible and can be used in all functions of your program. Warning: If you want to access and modify global variables, you must use the global keyword.
The following works:
from miniworlds import *
world = World((100,100))
a = 3
@world.register
def on_key_pressed_a(self):
print(a)
world.run()
The value 3 is output.
Warnung
This does not work, however, because when assigning, a is interpreted as a local variable (which was not defined)
from miniworlds import *
world = World((100,100))
a = 3
@world.register
def on_key_pressed_a(self):
a = a + 1
print(a)
world.run()
This works again because a is defined as a global variable and therefore the global variable is also accessed.
from miniworlds import *
world = World((100,100))
a = 3
@world.register
def on_key_pressed_a(self):
global a
a = a + 1
print(a)
world.run()