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

6.1a

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:

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

Code Examples

Resources