variablen
Deutsch   English   

6. VARIABLES

 

 

YOU LEARN HERE...

 

the important concept of variables. In computer science, variables are names that serve as placeholders for values that can change during program execution.

 

 

EXAMLES

 

So that you can change the program flexibly, you want to define the number of corners of an n-corner with a variable n. To do this, you make an assignment, for example n = 7 for a heptagon. The name n is linked to the number 7 and n can then be read several times in the program, but can also be changed.

 

Program:    

from gturtle import *

makeTurtle()
n = 7
setPenColor("blue")
repeat n:
    forward(100)
    right(360 / n)
► Copy to clipboard

ou can now easily draw any other n-corner, whereby you must also adjust the side length in each case so that the figure is not too large. You can even enter the values for n after starting the program. With the instruction
n = inputInt("Enter the number of corners")

opens an input dialog where you can enter a number. When you press the OK button, the dialog box closes and the entered value is assigned to the variable n.

 

Program:    

from gturtle import *

makeTurtle()
setPenColor("blue")
n = inputInt("Gib die Anzahl Ecken an:")
repeat n:
    forward(500 / n)
    right(360 / n)
► Copy to clipboard

The value of the variable can also change during the course of the program by reassigning it. For example, for the side length size of a square:

ou can even use the old value for the new assignment, i.e. write size = size + 50. You have to imagine that the following happens step by step:

  • the old value 20 of size is transferred to an arithmetic unit
  • the number 50 is added to it
  • the new value 70 is stored again as size.

Program:    

from gturtle import *

makeTurtle()
size = 40
repeat 4:
   forward(size)
   right(90)

size = size + 50
repeat 4:
   forward(size)
   right(90)
► Copy to clipboard
 

 

This notation is very elegant. For example, you can use it to have the Turtle draw 10 nested squares with a short program:

Program:    

from gturtle import *

makeTurtle()
size = 20
repeat 10:
    repeat 4:
        forward(size)
        right(90)
    size = size + 20
► Copy to clipboard
 

 

 

REMEMBER...

 

A variable is created when you assign a value to it using the equals sign. You can change its value at any time with a new assignment and even use its own (old) value. You must not confuse the notation used with a mathematical equation. The program statement:

n = n + 1

has nothing to do with a mathematical equation, i.e. with the task of determining n in such a way that the same value results to the left and right of equal signs.

For n = n + 1 there is also the short form n += 1, which does exactly the same thing.

 

 

TO SOLVE BY YOURSELF

 
1.
You are to draw strings of beads, whereby the number of beads is determined with an input dialog
n = inputInt("Number of beads?")
is entered. You use the command dot(20) and calculate the rotation angle with 360/n as in the first two program examples. Test your program for numbers between 20 and 30.
 

2.

With variables you can create beautiful graphics with little effort. The picture on the right is created by the Turtle moving forward 100 times s steps and rotating with the rotation angle w, increasing s by 1 with each pass. You start with s = 5. You enter the angle of rotation with an InputDialog:
w = inputInt("Enter the angle of rotation")
Test the program with different rotation angles (e.g. 118, 70, 89).

 

3.

The pretty figure on the right is created by the Turtle drawing 100 squares with side length s. With each run, the side length is increased by 2 and the turtle is rotated by 6 degrees.