design, write and debug programs that accomplish specific goals, including controlling or simulating physical systems; solve problems by decomposing them into smaller parts.

use logical reasoning to explain how some simple algorithms work and to detect and correct errors in algorithms and programs

## LO: To use and update variables in a program.

We have written a number guessing game back in 5.2b and we noted some improvements that could be added to make the game more fun.
Here is the comlpeted code for the game, make sure you remember how each part of the game works - look back at year 5 if you need to:

```
#declare a flag variable
play = True #we say it is True because we want the game to start

#declare a variable to reference our mystery number
mystery_num = 14

#introduce
print("This game will ask you to guess a mystery number between 0 and 20.")
print("") #this is just a blank line to make it look clearer

#start the game loop
while play:
#get input
user_input = input("Enter a number between 0 and 20 :")

#cast to int
guess = int(user_input)

#compare the guess with mystery number
if guess == mystery_num:
play = False
if guess > mystery_num:
#respond to user
print("Too big, try again")
if guess < mystery_num:
#respond to user
print("Too small, try again")

else:
print("Well done, you guessed the mystery number!")
print("Thanks for playing.")

```

To help us recap what we have done, let's look at some of the code we have used and why:

• play = True - here we have used a flag variable to function as a start/stop switch for the game; if it is True then the game runs, if it is false then the game is over.

• mystery_num = 14 - we use a variable to 'remember' the secret number.

• print() - the print function is used throughout the program to communicate with the user.

• user_input = input("Enter a number between 0 and 20 :") - the variable user_input is used to remember what the user guesses, the input() function is used to get the users guess.

• guess = int(user_input) - because the input() function returns a string the int() function casts it into an int and the guess variable 'remembers' this new int.

• while play: - the while condition checks if the play variable is True, if it is then the game loop is started. If it is False then the loop breaks and the game is over.

• if guess == mystery_num: - the if condition checks if the guess variable is the same as the mystery_num variable.

• if guess > mystery_num: - the if condition checks if the guess variable is bigger than the mystery_num variable.

• if guess > mystery_num: - the if condition checks if the guess variable is smaller than the mystery_num variable.

One suggesstion on how to improve this game was adding a limit to the number of guesses the user can have. Before we look at that improvement, let's look at how we can change variables so that we can count the number of guesses the user has made.
What we are looking for is a variable to 'remember' the number of guesses and a way to increase that number after each guess. The first one is easy, just declare a variable:

```
number_of_guesses = 0

```

Next we have to update that number, we need to change the variable by adding 1 to the original number that the variable is refering to:

```
number_of_guesses = number_of_guesses + 1

```

As we want to update the number_of_guessses variable after each guess we will need to include this in the game loop:

```
#start the game loop
while play:
#update the variable
number_of_guesses = number_of_guesses + 1
#get input
user_input = input("Enter a number between 0 and 20 :")

#cast to int
guess = int(user_input)

#compare the guess with mystery number
if guess == mystery_num:
play = False
if guess > mystery_num:
#respond to user
print("Too big, try again")
if guess < mystery_num:
#respond to user
print("Too small, try again")

```

Can you think why we have updated the variable at the start of the loop? Think what would happen if the user guessed the number on their first go...

We can also include this information (number of guesses) once the user has won the game:

```
else:
print("Well done, you guessed the mystery number!")
print("The mystery number was", mystery_num)
print("You guessed the number in", number_of_guesses, "guesses!")
print("Thanks for playing.")

```

Remember that when we use commas to separate the arguments, spaces will automatically be added. The complete code can be found in the code examples below as 6_guessingGame_with_counter.py