Guess the number game

 import random


def guess_number():

    number = random.randint(1, 100)

    attempts = 0

    

    print("Welcome to the Number Guessing Game!")

    print("I'm thinking of a number between 1 and 100.")

    

    while True:

        guess = int(input("Take a guess: "))

        attempts += 1

        

        if guess < number:

            print("Too low! Try again.")

        elif guess > number:

            print("Too high! Try again.")

        else:

            print(f"Congratulations! You guessed the number in {attempts} attempts.")

            break


guess_number()



In this game, the program generates a random number between 1 and 100. The player needs to enter their guess, and the program will provide feedback if the guess is too low or too high. The game continues until the player guesses the correct number. At the end, it displays the number of attempts it took for the player to guess the correct number.

You can modify this code to create your own game by changing the game rules, adding more functionality, or creating new features.

Comments

Popular posts from this blog

Ecommerce website

Yes, Python is an object-oriented programming (OOP) language, but it is also a multi-paradigm language

Your task is to find the missing number.