Guess the number (High / low game) - Python lesson
Guess the number tutorial
- Understand and use sequence in an algorithm
- Understand and use iteration in an algorithm (FOR and WHILE loops)
- Understand and use selection in an algorithm (IF, Else and Else if)
EDUCATOR: COMPUTATIONAL THINKING COMPETENCIES:
COMPUTER SCIENCE EDUCATORS:
CSTA K–12 CS Standards:
AREA OF LEARNING AND EXPERIENCE: Science and Technology:
Computation is the foundation for our digital world.
Progression step 3
- I can use conditional statements to add control and decision-making to algorithms.
- I can identify repeating patterns and use loops to make my algorithms more concise.
- I can explain and debug algorithms.
Progression step 4
- I can decompose given problems and select appropriate constructs to express solutions in a variety of environments.
- I can select and use data structures that efficiently manage data in algorithms.
- I can plan and implement test strategies to identify errors in programs.
Progression step 5
- I can identify, define and decompose problems, choose appropriate constructs and express solutions in a variety of environments.
- I can use file-handling techniques to manipulate data in algorithms.
- I can test, evaluate and improve a solution in software.
How the game works
The computer will think of a random number between 1 and 20, and ask the player to guess it. The computer will inform the player whether each guess is too high or too low. The player wins if they can guess the number within six attempts.
Before creating their guess the number game, have the students explore some of the functions they will be using.
Start by sharing the following program to compare two numbers and return a value 'higher' or 'lower' for the second number.
Challenge the students to predict what will happen before revealing the answer (You can test and run this code in the trinket window below).
Num1 = 5
Num2 = 10
if Num2 > Num1:
- Have the students copy the code then modify it by changing the two variables (Num1 and Num2) to see what happens.
Explain to the students that there is a problem with our code. Tell the students that if Num1 is the same as Num2, the program will display the message 'Lower'.
- Have the students modify their program so that it displays the message 'Same' if both numbers are the same.
Hint: The students will need to use an 'elif' statement.
Num1 = 5
Next, share the following sample code to simulate a roll of a dice. This will form the basis of the 'guess the number' game.
As with the previous exercise, challenge the students to first predict what will happen before revealing the answer (You can test and run this code in the trinket window below).
number = random.randint(0,3)
- Have the students copy the code then modify it so that it picks a number between 1 and 6.
- Ask students to comment on their code, using the hashtag (#), to explain what each line is doing.
Now that the students have all the components they need, challenge them to create their very own guess the number game.
number = random.randint(1, 10)
player_name = input("Hello, what's your name?")
number_of_guesses = 0
print("Okay " + player_name + ", I am Guessing a number between 1 and 10:")
while number_of_guesses < 5:
guess = int(input("What is your guess? "))
number_of_guesses += 1
if guess < number:
print("Your guess is too low")
elif guess > number:
print("Your guess is too high")
if guess == number:
print("Congratulations! You guessed correctly.")
print("You did not guess the number, the number was ")
- Students to modify their code so that it tells the player how many guesses they made.
- Students to change the random number from 1-10 to 1 - 100 (Students must also remember to change the message at the start of the game!).
- Students to modify their code so that it keeps looping the game until the player quits and also keeps a track of the player's best score (least number of guesses).
Unless otherwise specified, everything in this repository is covered by the following licence: