How to Create a Number Guessing Game in Python Step-by-Step Tutorial

0

Create a Number Guessing Game in Python

The number guessing game is a favorite among programmers. In this game, the program selects a random number within a specified range, and the user attempts to guess the correct number. If you're interested in learning how to create a guessing game using Python, this article is perfect for you. In this tutorial, I will guide you through the steps to create a number guessing game using Python

Create a Number Guessing Game in Python Step-by-Step Tutorial
Create a Number Guessing Game in Python Step-by-Step Tutorial

We will cover everything from setting up the environment to writing the code and adding enhancements to make the game more engaging. By the end of this tutorial, you'll have a fully functional game and a better understanding of Python programming concepts.

Code :
import random

def number_guessing_game():
    n = random.randint(1, 10)
    attempts = 0

    print("Welcome to the Number Guessing Game!")
    print("I have chosen a number between 1 and 10. Try to guess it!")

    while True:
        guess = input("Enter your guess (or type 'quit' to exit): ")

        if guess.lower() == 'quit':
            print("Game over. The number was:", n)
            break

        try:
            guess = int(guess)
        except ValueError:
            print("Invalid input. Please enter a number between 1 and 10.")
            continue

        attempts += 1

        if guess < n:
            print("Too low! Try again.")
        elif guess > n:
            print("Too high! Try again.")
        else:
            print(f"Congratulations! You guessed it right in {attempts} attempts!")
            break

number_guessing_game()
OutPut :
Welcome to the Number Guessing Game!
I have chosen a number between 1 and 10. Try to guess it!
Enter your guess (or type 'quit' to exit): 4
Too high! Try again.
Enter your guess (or type 'quit' to exit): 3
Too high! Try again.
Enter your guess (or type 'quit' to exit): 87
Too high! Try again.
Enter your guess (or type 'quit' to exit): 54
Too high! Try again.
Enter your guess (or type 'quit' to exit): quit
Game over. The number was: 1
Features of this code :

  1. The game generates a random number between 1 and 10 using random.randint(1, 10).
  2. It prompts the user to enter their guess.
  3. It handles inputs gracefully by checking if the input is numeric and within the valid range.
  4. If the user types 'quit', the game ends and reveals the correct number.
  5. It counts the number of attempts made by the user before guessing correctly.
  6. It provides feedback ('Too low!', 'Too high!') based on the user's guess.
  7. Once the correct number is guessed, it congratulates the player and displays the number of attempts.
You can run this script in a Python environment to play the number guessing game interactively.

190 Python Projects for All Levels:
190 Python Projects for All Levels:


Post a Comment

0Comments
Post a Comment (0)

#buttons=(Accept !) #days=(20)

Our website uses cookies to enhance your experience. Learn More
Accept !
✨ Updates