# Python in the classroom - lesson 3

In the previous lessons we worked with text (strings) and numbers (numbers). We also looked at how you can execute code conditionally. You can do that using if..elif..else. Do you remember? If not, it might be useful to review lesson 1 and lesson 2 first!

In this lesson we are going to program our first game! We are going to make a guessing game where we have to guess a number between 1 and 10. But before we can do that, we need to cover a few more things.

## While loop

We often want the computer to repeat certain steps. Consider, for example, the guessing game we are going to make. The program should always do the same thing if the number is not guessed. The program must always ask for a number as input and then check whether the number is correct. If not, it has to ask for another number. Of course it is not useful if we have to write new lines of code for this, which actually do the same thing. And also think about what it would mean if you didn't have to guess a number between 1 and 10, but between 1 and 100. Then you would suddenly have to write 90 more code blocks. A number between 1 and 1000 is a complete disaster.

There are, of course, ways to do this too conveniently. The while loop is perfect for this scenario. The "while loop" keeps repeating itself until the condition is no longer met. Below an example of a while loop.

``````x = 0

print(x)

while x < 10:
x = x + 1

print(x)``````

We now get the output 0 and on the next line 10. What happened here?

First we create a variable x. This is equal to 0. We print x and logically get 0 as output. Then we start with a "while loop". At the top of the while loop is the condition. So the code repeats as long as x is less than 10.

Below, indented, is the code that will run with each iteration. Each time we set x equal to x + 1. So the first time this rule is executed, it says x = 0 + 1. After the first rule x is therefore 1. Python then checks the condition, and this is true because 1 is less than 10. So the code is executed again. The second time the while loop repeats, it says x = 1 + 1, because x is at the beginning of the second repeat 1. After the second repeat, x = 2. The condition is checked again and is true again, because 2 is less than 10. This continues until the ninth repetition. At the beginning of the 9th repetition is x = 9 and at the end of the 9th repetition x = 10 (make sure you understand this!). Python then check the condition, and it is not true (False) because 10 < 10 is not true. So the code is not repeated and Python continues below the while loop.

Below the while loop is the last line of code (print). Since x = 10 we see a second output, namely 10.

Good to know: adding 1 to a number is often used in Python. For example, it is very useful to keep a count of how many times a loop has been repeated, or to stop the loop using it. That is why there is a "shorthand" notation for x = x + 1, namely x + = 1.

``````x = x + 1

#same result

x += 1``````

To complete the guessing game, we still need to be able to enter a number. How can you do this?

You can request input by means of the “input” function.

``input('your name')``

You will then see an input field appear where you can enter text.

It is of course useful to also save what is entered here. Because you probably want to do something with this. So you have to make a variable with this input!

``````name = input('your name')

print(name)``````

Now you see that what is entered can also be used in later lines of code. Because the print statement now states what has been entered in the input!

For our guessing game there is another complication: input is a string (text) by default. Even if you enter a number, Python will still store it as a string. After that it is not immediately possible to compare the input with the random number. You have to convert it to a number first.

``````number_as_string = input('give a number between 1 and 10')

number = int(number_as_string)``````

We first ask for a number between 1 and 10. However, this is a string (text) by default! We can't yet use this to compare the number to our random number. The function “int” converts the string (text) to an integer (whole number). Now the variable “number” is suitable for making the comparison.

## Import statements

We talked about why it is useful to use Python in Lesson 1. One of the reasons mentioned at the time was the availability of many tools or “libraries”. They are also called “packages”. What are those “libraries”? And how can you use them?

Python Libraries offer all kinds of functionalities that you can use in your code. There are literally too many to list in total, but let's take it as an example Matplotlib .

With the help of Matplotlib you can create beautiful graphs in your Python code. For example this graph “Scores by group and gender”. It would be very difficult to program such a graph from scratch. Fortunately, Matplotlib offers this functionality and you can still make such graphs with a few lines of code!

Besides Matplotlib there are thousands of other useful “libraries”. Other well-known libraries are NumPy to do math calculations and Tkinter to create user interfaces. There are many “libraries” that can be used for free, so-called Open Source projects. These libraries are maintained by a group of developers, often on a voluntary basis. Sometimes they are supported by large companies. Not all libraries or packages are free, there are also commercial ones. For this you need a paid license.

We will now use a package / library called "random". We will use this to pick a random number at the start of our game. First you import random in the following way:

``import random``

After the import statement you can use all functions that random offers. The function we are going to use is “randint”. This is a function that chooses a random integer between the values ​​you choose. Below we choose a random number between 1 and 10.

``````import random

x = random.randint(1,10)

print(x)``````

If you click the play button several times, you will see a different number between 1 and 10. How does this work? First we import random. We then create a variable x that we set equal to random.randint (1,10). This may look very strange. Randint is a function of the random package. Since we imported random, we can use this function. But how do you know which functions random can perform? This is a matter of reading the documentation or googling. At the random documentation states:

`random.``randint`(ab)

Return a random integer N such that `a <= N <= b`. Alias for `randrange(a, b + 1)`.

Often you start by asking yourself what you want to do. You go googling and you will (hopefully) find a handy library that can do this for you. This saves a lot of work, because you don't have to program it all yourself! If you have chosen the best library, then you read the documentation carefully and you can use the function properly!

## Guessing game

And now we have enough information to start making the guessing game. The assignment is as follows:

Program a guessing game where you have to guess a number between 1 and 10. The program first chooses a number between 1 and 10 using a random function. The program then runs a loop asking for input each time. The program stops when the correct number is guessed. You then indicate with the help of a print statement that the correct number has been guessed.

Try it yourself first! It is still quite difficult. The solution (rather, a solution) for the guessing game can be found in this Python notebook and also below:

``````import random

x = random.randint(1,10)

y = 0

while x != y:
z = input('number between 1 and 10')
y = int(z)

print('you have guessed it! the right number was indeed ' + str(x))``````

We first import random. We choose a random number between 1 and 10 and store this number in the variable x. Then we create a new variable y and first set it equal to 0 (so that the while loop will execute at least once). You will often see students skip this step (and you may have done this yourself). You then get the problem that the variable you want to compare with is not yet available to make the while loop. The lines within the while loop can optionally be merged. You can also write y = int (input ("number between 1 and 10")). This way the input is converted directly to an integer.

Some students will try to make if..else .. statements to see if input should be requested or if the correct number should be printed. However, this is not necessary, because you can use the while loop for this! The while loop stops when the number is guessed. Below that you can write the line of code to let you know that the correct number has been guessed.