Week 3 — Number Guessing
Instructor: Ezekiel Manji Samuel
Game Chrips Academy · Every Thursday
Student Notes · Python Game Development
Name: _______________________________ Date: _______________
What You'll Learn Today
■ How to use the random module to make your game unpredictable
■ How a while loop keeps a game running until something happens
■ How if/elif/else gives the player feedback on their guess
■ How to build and run a complete game from scratch
The Full Game — Read This First
This is the complete code we built in class. Read it top to bottom before looking at the explanation below.
import random
secret = [Link](1, 10)
lives = 3
while lives > 0:
guess = int(input('Guess a number (1-10): '))
if guess == secret:
print('Correct! You win!')
break
elif guess < secret:
print('Too low! Try again.')
else:
print('Too high! Try again.')
lives = lives - 1
print('Lives left:', lives)
if lives == 0:
print('Game Over! The number was', secret)
■ Tip: Try to understand the full picture first, then read the line-by-line breakdown below.
Part 1 — Setting Up the Game
These are the first lines that run when you start the program. They prepare everything the game needs.
Code What it does
import random This brings in Python's random toolbox. Without this line the
computer doesn't know how to pick random numbers.
secret = [Link](1, 10) This picks a random whole number between 1 and 10 and stores
it in a variable called secret. The player has to guess this number.
lives = 3 This gives the player 3 chances to guess. Every wrong guess
reduces this number by 1.
■ After these 3 lines: the game has a secret number ready and the player has 3 lives. Nothing has happened on
screen yet.
Part 2 — The Game Loop (while)
The while loop is the engine of the game. It keeps asking for guesses as long as the player still has lives.
Code What it does
while lives > 0: This means: keep repeating everything inside this block as long as
lives is greater than 0. When lives reaches 0, the loop stops.
guess = int(input('Guess a number input() asks the player to type something. int() converts what they
(1-10): ')) typed into a number. Without int(), Python sees '5' as text, not a
number — and can't compare it.
■ The 4 spaces before each line inside the loop are called indentation. Python uses this to know which lines are
inside the loop. Always indent!
Part 3 — Checking the Guess (if / elif / else)
After the player types their guess, the game checks if it's correct, too low, or too high.
Code What it does
if guess == secret: Checks if the player's guess is exactly equal to the secret number.
Note: == means 'is equal to'. A single = means 'set this value' —
they are different!
print('Correct! You win!') If the guess was correct, this message is printed on screen.
break break immediately exits the while loop. The game stops — the
player won. Without break, the loop would keep going even after a
correct guess.
elif guess < secret: If the first condition was False, this checks if the guess was too
low. elif means 'otherwise, if'.
print('Too low! Try again.') Tells the player their guess was too low so they can adjust.
else: If none of the conditions above were True, this runs. At this point
the only remaining possibility is that the guess was too high.
print('Too high! Try again.') Tells the player their guess was too high.
■ == is for comparing. = is for storing. if guess = secret will give you an error. Always use == inside if statements.
Part 4 — Losing a Life
After every wrong guess, the player loses one life and the remaining lives are shown on screen.
Code What it does
lives = lives - 1 Takes one life away from the player. If lives was 3 it becomes 2. If
it was 1 it becomes 0 — and the while loop stops next time it
checks.
print('Lives left:', lives) Shows the player how many lives they have remaining after their
wrong guess.
■ After this the while loop goes back to the top and checks: is lives still greater than 0? If yes — ask for another
guess. If no — stop.
Part 5 — Game Over
This part runs after the while loop ends — either the player won (break) or ran out of lives.
Code What it does
if lives == 0: Checks if the player ran out of lives. If they guessed correctly and
used break, lives won't be 0 so this message won't show.
print('Game Over! The number was', Reveals the secret number to the player so they know what they
secret) were trying to guess. Printing a variable next to text like this is
called string concatenation with a comma.
■ If the player guesses correctly, break exits the loop before lives reaches 0. So the Game Over message only
shows when the player loses.
How the Game Flows — Step by Step
Here is exactly what happens from start to finish when you run the game:
Step What happens
1 Python imports random and picks a secret number between 1 and 10
2 lives is set to 3
3 The while loop starts — lives is 3 which is greater than 0 so it runs
4 The player is asked to type a guess
5 The game checks if the guess is correct, too low, or too high and prints feedback
6 If correct → break exits the loop. Player wins.
7 If wrong → lives loses 1. Remaining lives are shown.
8 The while loop goes back to Step 3 and checks lives again
9 When lives reaches 0 → loop ends → Game Over message shows the secret number
Try It Yourself — Challenges
Now that you understand the code, try making these changes on your own:
■ Easy Change the number range from 1–10 to 1–20
■ Easy Give the player 5 lives instead of 3
■■ Medium Count how many guesses the player used and show it when they win
■■
Hard Add a scoring system — more points for guessing correctly with more lives remaining
■
Keywords from This Session
Keyword Meaning
import random Loads Python's random number tools into your program
[Link](a, b) Picks a random whole number between a and b (including both)
int(input()) Asks the user to type something and converts it to a number
while condition: Keeps repeating the block inside as long as the condition is True
break Immediately exits the loop, no matter what the condition says
== Checks if two values are equal. Different from = which stores a value
lives = lives - 1 Takes 1 away from lives and saves the new value back into lives
Robotics & AI for Young Minds · Week 3 · Chrips Academy Ezekiel Manji Samuel · ezekielmanji645@[Link]