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
5.2b
LO: To use conditional statements with user input. (2)
Let's look at writing another small program that uses conditional statements with user input. Last time we saw how we can use if statements to respond to different letters the user inputs, this time we are going to make a number guessing game so we will be responding to numbers that the user inputs.
Here's the game: The user is asked to guess a mystery number, at each attempt they are told if their guess is either too small or too big.
Now think about all we know so far and let's come up with some ideas of how to write this game:
We have to start with some instructions explaining the game.
We have to use a game loop that will run until the game is over (when the correct guess is made).
We need a variable to hold the mystery number.
We need to get user input (their guesses) and evaluate them - are they too high or two low or correct and respond to the user.
The game needs to end if the guess is correct.
These seem like a good set of instructions for us to follow to create the game. Anything missing?
One important difference from our silly sentence game is that we are dealing with numbers... and we all know that input() returns a string, so we will need to cast the strings to ints everytime the user makes a guess. Ok, let's start with our basic game setup that be used in the silly sentence game but adapted for our new guessing game:
#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: else: print("Well done, you guessed the mystery number!") print("Thanks for playing.")
Hopefully this will be clear to follow:
The flag variable play will be responsible for starting and stopping the game loop.
The variable mystery_num will hold our mystery number.
The introduction will explain the game rules.
The while loop will be our game loop.
The else statement will be run at the end of the game when the play variable is False.
Now think how you would write code to run the game inside of the while loop: get input, cast to int, compare the guess with the mystery number, respond to the user or end the game if correct...
#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.")
Notice that we don't need an else statement inside of our loop.
Putting this together we get*:
#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.")
* Children may suggest improvements to this game - like only giving a set number of guesses etc. This game will be improved later on (year 6).