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 THE 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 example: the int function
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:
The above line will draw* a number from the range between 1 and 10.
See example: Drawing
* 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.
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:
as long as the condition is fulfilled:
do the body of the loop
a = 1
while a < 10:
print("a =", a)
a += 1
How does it work?
In the first line we declare the variable a, we give it the value of 1,
Then we have the declaration of the while loop: it will be executed until a is less than 10. As soon as a reaches the value of 10, Python will exit the loop and move further.
In the next two lines (remember about the colon and indentation!) the body of the loop is executed: the value of the variable a is displayed, and then the variable a is incremented.
If not for the last line (a += 1), the variable would never change its value and therefore the condition a < 10 would always be true. This would result in the infinite execution of the body of the loop.
See example: Loop while
You can start writing the game!
First import the randint function from the random library (see above).
Then draw* a number from 1 to 10 and assign it to the riddle variable.
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.
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. 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 to the task
** You can write the same thing in a better way: not guessed. I decided to write it because I thought it would be more readable for a novice programmer.