« Seznam kurzů

Write your first game!

Look how much we've learned! We already know all the programming elements needed to write something on our own: for example, a simple guessing game. The rules will be simple: the computer will come up with a number in the range from 1 to 10, and the task of the player will be to guess the number. The computer will tell us if our attempt was correct, or if the guessed number is greater or less than the number chosen by the computer.

Before we do that, we have to learn three more simple things:

Retrieving data from a user

In Python there is an input command, which takes a string as a parameter. This string is a message that will be displayed, and then the computer will wait for the user's response. After entering the information and pressing Enter, the input function returns the value that can be assigned to a variable.

my_data = input("Enter your age: ")

After following this instruction, the my_data variable will store the value entered by the user from the keyboard.

Note: this value is always a string, so we need to convert it to a number:

my_age = int(my_data)

The int function will attempt to convert the string to a number. If it fails, it will throw a ValueError. Those strings that consist only of digits, e.g. "11", "485", "228", but "11a", "1667.3" will be converted correctly, but "I am your father!" - will not! Of course, Python allows you to protect yourself against such erroneous values, but we won't deal with it in our tutorial because we would complicate it too much.

See an example of the int() function

Importing

Python is distributed with a huge standard library. These additional functions, grouped into different sets, are not loaded into memory all at once it would take up much of the computer resources. They are, therefore, available at any time, but upon request - we need to import them.

To make an import, we write at the beginning of the program:

from <library> import <function>

Why are we mentioning this now?

Because this is how we will incorporate the randint function into our program, which will draw*) a natural number from the range we provide. This function is located in a library called random. So in order to import it, we need to write the following line at the beginning of the program:

from random import randint

Once we've done that, we can immediately use it:

print(randint(1,10))

The above line will draw *) a number from the range between 1 and 10.

See an example of a random number printing

*) In fact, the computer does not draw a number, it calculates it on the basis of a large set of data that constantly change (e.g. the time of a system clock). That is why such "drawn" numbers are called pseudo-random numbers.

While Loop

In the chapter on loops, we learned how to create for - in loops, which are the most common way to loop our code. There is another loop in Python: while.

The while loop works as follows:

a = 1
while a < 10:
    print("a =", a)
    a += 1

How does it work?

See example of ‚Loop while‘

You can start writing the game!

  1. First import the randint function from the random library (see above).
  2. Then draw*) a number from 1 to 10 and assign it to the riddle variable.
  3. Declare the guessed variable - give it the False logical value. This will be the so-called flag that we will raise (True) when the player will have guesses the number.

    Now it's time for something more difficult: start writing the while loop. Let's make this loop dependent on the condition guessed == False**). This means that the loop will be executed as long as the value of this variable is false, i.e. until the player has guesses the number. When they've guesses it, we will raise the flag and then Python will exit the loop.

  4. In the body of the loop we take the response from the user, convert it from a string to a number, and check the following conditions:
    • if the answer is less than the drawn number, we display the message "too little!”
    • if the previous condition is not met, but the answer is greater than the drawn number - we display the message "too much!”
    • if both the above cases are not true, it means that the user entered the correct number.
  5. We should then:
    • display the message "Great, that's the right number!",
    • assign True value to the guessed variable; this will render the condition for executing the while loop unfulfilled and the program will exit the loop.

Done! You wrote your first program, congratulations!

Need a hint?

See the solution

**) You can write the same thing in a better way: not guessed. We decided to write it because I thought it would be more readable for a novice programmer.