Search⌘ K
AI Features

Project: Number Guessing Game (with Personality)

Explore how to build your own interactive number guessing game using Python. Learn to generate random numbers, receive and evaluate user guesses, and provide friendly feedback that keeps players engaged. This project helps you practice loops, conditionals, and user input while adding personality to your code.

Ready to test your Python skills and have fun? Create your own interactive number guessing game that makes players laugh and think!

In this project, the computer will pick a secret number, and players will guess it while receiving playful feedback and hints.

Goals

You’ll aim to:

  • Generate a random number with Python’s random module.

  • Take user guesses and provide engaging feedback.

  • Track the number of guesses and celebrate wins.

Project steps

Let’s discuss the macro-level breakdown of the project that we will discuss in steps:

  • Use Python’s random module to generate a number between 1 and 100.

  • Ask the player to guess the number using input() and convert it to an integer.

  • Give feedback like “Too high” or “Too low” using if, elif, etc.

  • Keep the game going until the player guesses the correct number.

  • Add a loop to replay the game and track the fewest attempts.

Step 1: Game setup

Import the random module, display a welcome message, and generate the secret number. Also, set up a variable to count the number of guesses.

import random

print("🎉 Welcome to the Number Guessing Game!")
print("I'm thinking of a number between 1 and 100. Can you guess it?")
secret_number = random.randint(1, 100)
attempts = 0
Game setup

Step 2: User input

Start a loop that keeps asking the user to guess the number. Each time they guess, increase the number of attempts.

while True:
    guess = int(input("Take a guess: "))
    attempts += 1
Take the user input

Step 3: Feedback and encouragement

Check if the guess is too low, too high, or correct using if, elif, and else. Give friendly hints and break the loop if the guess is right.

    if guess < secret_number:
        print("Too low! But nice try! 🔽")
    elif guess > secret_number:
        print("Too high! You're flying too close to the sun! 🔼")
    else:
        print(f"🎉 Congrats! You guessed the number in {attempts} attempts!")
        break
Decision based on input

Project tips

  • Keep your hints positive and humorous.

  • Consider tracking scores or attempts to make it competitive.

  • Celebrate the player's success enthusiastically!

Mini challenge

  • Allow the user to set the difficulty (number range).

  • Track the best (fewest) number of attempts and display it.

best_score = None

play_again = "yes"
while play_again.lower() == "yes":
    # (Your guessing game here)

    if best_score is None or attempts < best_score:
        best_score = attempts
        print(f"🏅 New record! Fewest attempts: {best_score}")

    play_again = input("Want to play again? (yes/no): ")
    if play_again.lower() != "yes":
        print("Thanks for playing! Come back soon!")
Challenge: Number guessing game

If you’re stuck, click the “Show Solution” button.

Go further

  • Add difficulty levels (easy, medium, hard).

  • Provide more detailed hints as guesses get closer.

Good job! Have fun creating your playful guessing game, and bring joy to everyone who plays!