Uncategorized

Number Guessing Game in Python


Introduction

The number guessing game is a popular game among programmers. In the number-guessing game, the program selects a random number between two numbers, and the user guesses the correct number. You can use an online editor. I have used Google Colab https://colab.google/

Method 1. Basic Game with a Limited Number of Guesses

import random

number_to_guess = random.randint(1, 100)
attempts = 0
max_attempts = 5

print("Welcome to the Number Guessing Game!")
print("I'm thinking of a number between 1 and 100. You have 5 attempts to guess it.")

while attempts < max_attempts:
    try:
        guess = int(input("Make a guess: "))
        attempts += 1

        if guess < number_to_guess:
            print("Too low.")
        elif guess > number_to_guess:
            print("Too high.")
        else:
            print(f"Congratulations! You've guessed the number in {attempts} attempts.")
            break
    except ValueError:
        print("Please enter a valid number.")

if attempts == max_attempts:
    print(f"Sorry, you've run out of attempts. The number was {number_to_guess}.")

Output

Number guessing game

Method 2. Game with Difficulty Levels

#Method 2
import random

print("Choose difficulty level: Easy (1-50), Medium (1-100), Hard (1-200)")
difficulty = input("Enter difficulty (Easy, Medium, Hard): ").lower()

ranges = {
    'easy': 50,
    'medium': 100,
    'hard': 200
}
number_to_guess = random.randint(1, ranges.get(difficulty, 100))

print(f"Welcome to the Number Guessing Game! I'm thinking of a number between 1 and {ranges.get(difficulty, 100)}.")

while True:
    try:
        guess = int(input("Make a guess: "))

        if guess < number_to_guess:
            print("Too low.")
        elif guess > number_to_guess:
            print("Too high.")
        else:
            print("Congratulations! You've guessed the number.")
            break
    except ValueError:
        print("Please enter a valid number.")

Output

Difficult level

Method 3. Game with Hint System

#Game with Hint System
import random

number_to_guess = random.randint(1, 100)

print("Welcome to the Number Guessing Game!")
print("I'm thinking of a number between 1 and 100. I'll give you a hint after each wrong guess.")

while True:
    try:
        guess = int(input("Make a guess: "))

        if guess < number_to_guess:
            print("Too low.")
            if number_to_guess % 2 == 0:
                print("Hint: The number is even.")
            else:
                print("Hint: The number is odd.")
        elif guess > number_to_guess:
            print("Too high.")
            if guess % 2 == 0:
                print("Hint: Try an odd number.")
            else:
                print("Hint: Try an even number.")
        else:
            print("Congratulations! You've guessed the number.")
            break
    except ValueError:
        print("Please enter a valid number.")

Output

Hint system

Conclusion

Now you finally get to play your guess-the-number game. You have learned the three methods to write the number guessing game in Python.



Source link

Leave a Reply

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