Lets Start Python Programming For Beginners to advanced. Building a Mini Game, Lectures 7
๐ Lecture 7: Building a Mini Game
๐ฏ Goal
By the end of this lecture, you will:
- Understand how to combine conditional statements and variables.
- Be able to create a simple number guessing game in Python.
- Learn how to give hints like “Too high” or “Too low”.
- Practice using loops and user input.
๐ง What Are We Building?
We’re going to build a fun Number Guessing Game , where:
- The computer picks a secret number.
- The player tries to guess it.
- The game tells them if they’re too high, too low, or correct!
๐ฏ This is a great way to practice:
if
,elif
,else
statements- Taking input with
input()
- Using comparison operators (
>
,<
,==
) - Loops (we’ll use a
while
loop)
๐ฎ Step-by-Step Game Plan
๐ข Step 1: Pick a Secret Number
Letโs start by setting a secret number for the player to guess:
secret_number = 7
This is the number the player has to guess.
๐งโ๐ป Step 2: Ask the Player to Guess
Use input()
to get the player’s guess:
guess = int(input("Guess the number (1โ10): "))
We use int()
to convert the input from text to a number so we can compare it.
โ Step 3: Compare the Guess
Now letโs check what the player guessed:
if guess == secret_number:
print("You got it right! ๐")
elif guess < secret_number:
print("Too low! Try again.")
else:
print("Too high! Try again.")
This uses our conditional statements to give feedback.
๐ Step 4: Keep Asking Until They Get It Right
Right now, the player only gets one try. Letโs change that with a while
loop:
secret_number = 7
while True:
guess = int(input("Guess the number (1โ10): "))
if guess == secret_number:
print("You got it right! ๐")
break
elif guess < secret_number:
print("Too low! Try again.")
else:
print("Too high! Try again.")
โ This loop keeps running until the player guesses correctly. Then it breaks out of the loop.

๐งช Try It Yourself!
Try playing your new game! Hereโs the full code again:
secret_number = 7
print("๐ฒ Welcome to the Number Guessing Game!")
while True:
guess = int(input("Guess the number (1โ10): "))
if guess == secret_number:
print("๐ You got it right! Great job!")
break
elif guess < secret_number:
print("๐ Too low! Try again.")
else:
print("๐ Too high! Try again.")
๐ Sample Output:
๐ฒ Welcome to the Number Guessing Game!
Guess the number (1โ10): 5
๐ Too low! Try again.
Guess the number (1โ10): 9
๐ Too high! Try again.
Guess the number (1โ10): 7
๐ You got it right! Great job!
๐ง Kids Corner
๐ง Imagine you’re playing hide-and-seek with a robot.
Every time you guess a number, the robot says:
- “You’re getting warmer!” if you’re close
- “You’re cold!” if you’re far away
That’s exactly what we’re doing โ helping the player find the hidden number!
๐ค Robot says:
You're getting closer... keep trying!
๐ Challenge (Optional)
Can you make these improvements to your game?
- Let the player choose the range (like 1โ20).
- Count how many guesses it took and tell them at the end.
- Add a โPlay Again?โ feature after winning.
๐ก Example Code for Counting Guesses:
secret_number = 7
guesses = 0
while True:
guess = int(input("Guess the number (1โ10): "))
guesses += 1
if guess == secret_number:
print(f"๐ You got it in {guesses} tries!")
break
elif guess < secret_number:
print("Too low! Try again.")
else:
print("Too high! Try again.")
๐ Summary
- We built a Number Guessing Game using:
- Variables to store data
input()
to get player choicesif
,elif
,else
to make decisions- A
while
loop to repeat until the player wins
- Games are a fun way to practice programming!
- You can improve the game with extra features like score tracking or difficulty levels.
Python Programming For Beginners To Advanced
Python Programming For Beginners! Lecture 1
Python Programming! Variables and Data Types | Lectures 2
Python Programming! Input And Output | Lectures 3
Python Programming! Operators in Python Lectures 4
Python Programming! Conditional Statements Lectures 5
Python! Loops For and While | Lectures 6
Python! Building A Mini Game | Lectures 7