Skip to content

Python! Building A Mini Game | Lectures 7

Lecture 7 Building a Mini Game

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.

[adrotate banner=”8″]

๐Ÿ” 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.

Lecture 7 Building a Mini Game
Lecture 7 Building a Mini Game

๐Ÿงช Try It Yourself!

[adrotate banner=”7″]

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)

[adrotate banner=”5″]

Can you make these improvements to your game?

  1. Let the player choose the range (like 1โ€“20).
  2. Count how many guesses it took and tell them at the end.
  3. 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 choices
    • if, 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

Najeeb Alam

Najeeb Alam

Technical writer specializes in developer, Blogging and Online Journalism. I have been working in this field for the last 20 years.

Leave a Reply

Your email address will not be published. Required fields are marked *