Number guessing 2

This entry is part 2 of 2 in the series Number Guessing
1
Python
import random

def number_guessing_game():
    print("๐ŸŽ‰ Welcome to the Number Guessing Game!")
    print("I'm thinking of a number between 1 and 100...")

    # Generate a random number between 1 and 100
    secret_number = random.randint(1, 100)
    attempts = 0

    while True:
        try:
            guess = int(input("Enter your guess: "))
            attempts += 1

            if guess < 1 or guess > 100:
                print("๐Ÿšซ Please enter a number between 1 and 100.")
                continue

            if guess < secret_number:
                print("๐Ÿ“‰ Too low! Try again.")
            elif guess > secret_number:
                print("๐Ÿ“ˆ Too high! Try again.")
            else:
                print(f"โœ… Congratulations! You guessed it in {attempts} attempts.")
                break

        except ValueError:
            print("โš ๏ธ Please enter a valid number.")
Series Navigation<< Number guessing 1

Comments

Leave a Reply

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