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:
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
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.
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?
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.
Done! You wrote your first program, congratulations!
Need a hint?
**) 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.