User manual
16
Fig. 1.9: 1.4.1 Guessing numbers with Python
1.4.2 How does it work?
Let’s try and see if the game works. Naturally some questions are coming up: What is happening in the
background? What are the individual command lines standing for in the programme?
import random To generate the random number, an external Python module called random is imported
which contains various functions for random generators.
number = random.randrange(1000) The function randrange of the module random generates a random
number in the number field which is defined by parameters, here a number between 0 and 999. The
parameter of the function
random.randrange() indicates the number of possible random numbers and
beginning with 0, it is therefore always the first number not guessed right. The same goes for loops and
similar functions in Python.
This random number is stored in the variable
number. Variables in Python are memory locations which can
have any name and can store numbers, strings, lists, or other data types. Unlike in some other programming
languages, they do not have to be declared in advance.