Chapter 4: Random Numbers and Games / Lesson 25

Number Guessing Game

🎯 Project: Number Guessing Game

Let's build an interactive number guessing game! This project combines everything you've learned: random numbers, loops, conditionals, and user interaction. You'll create a game where the computer picks a random number and the player tries to guess it.

This project will help you practice using random.randint(), while loops, if/elif/else statements, and input handling.

Project Requirements

Your number guessing game should:

  • Generate a random number between 1 and 100
  • Allow the player to make guesses
  • Provide feedback (too high, too low, or correct)
  • Continue until the player guesses correctly
  • Track and display the number of attempts

Step-by-Step Guide

Here's how to build the game step by step:

game_steps.py
# Step 1: Import random module import random # Step 2: Generate random number secret_number = random.randint(1, 100) # Step 3: Initialize guess counter attempts = 0 # Step 4: Create game loop while True: # Get player's guess (for this exercise, use a provided value) guess = 50 # Replace with actual game logic attempts += 1 # Compare guess with secret number if guess == secret_number: print(f"Congratulations! You guessed it in {attempts} attempts!") break elif guess < secret_number: print("Too low! Try again.") else: print("Too high! Try again.")

Complete Game Example

Here's a complete version of the game with all features:

complete_game.py
import random # Welcome message print("Welcome to the Number Guessing Game!") print("I'm thinking of a number between 1 and 100.") # Generate secret number secret_number = random.randint(1, 100) attempts = 0 # Game loop while True: # For this exercise, simulate guesses (replace with actual input) # guess = int(input("Enter your guess: ")) guess = random.randint(1, 100) # Simulated guess attempts += 1 print(f"Attempt {attempts}: You guessed {guess}") if guess == secret_number: print(f"🎉 Correct! You found it in {attempts} attempts!") break elif guess < secret_number: print("⬆️ Too low! Try a higher number.") else: print("⬇️ Too high! Try a lower number.") if attempts >= 10: print(f"Game over! The number was {secret_number}") break

Adding Features

Enhance your game with additional features:

enhanced_game.py
import random # Difficulty levels difficulty = "easy" # easy, medium, hard if difficulty == "easy": max_num = 50 max_attempts = 10 elif difficulty == "medium": max_num = 100 max_attempts = 7 else: max_num = 200 max_attempts = 5 secret_number = random.randint(1, max_num) print(f"Guess a number between 1 and {max_num}") print(f"You have {max_attempts} attempts!") attempts = 0 while attempts < max_attempts: attempts += 1 remaining = max_attempts - attempts print(f"Attempt {attempts}/{max_attempts} ({} remaining)".format(remaining)) # Game logic here break # Placeholder

Tips and Best Practices

✅ Game Design Tips

• Start with a simple version, then add features

• Provide clear, helpful feedback to players

• Track attempts to make the game more engaging

• Consider adding difficulty levels or ranges

• Add a limit on attempts for challenge

💡 Testing Your Game

Test your game with different scenarios: correct guess on first try, many attempts, edge cases (1, 100). Make sure the feedback messages are clear and helpful!

🎉

Lesson Complete!

Great work! Continue to the next lesson.

main.py
📤 Output
Click "Run" to execute...