Guess The Number Game in Python Source Code

Free

Guess the number game in Python Source Code


Python

Snapshots


Explore the excitement of coding with our 'Guess the Number Game in Python' source code. This beginner-friendly Python project lets you delve into the world of programming while building a fun and interactive guessing game. Enhance your coding skills, understand random number generation, and create a fully functional game with our easy-to-follow Python source code.



import random

def guess_the_number():
    # Generate a random number between 1 and 100
    secret_number = random.randint(1, 100)

    print("Welcome to Guess the Number!")
    print("I've picked a number between 1 and 100. Can you guess it?")

    attempts = 0

    while True:
        # Get the player's guess
        guess = int(input("Your guess: "))

        # Increase the number of attempts
        attempts += 1

        # Check if the guess is correct
        if guess == secret_number:
            print(f"Congratulations! You've guessed the number in {attempts} attempts.")
            break
        elif guess < secret_number:
            print("Too low. Try again!")
        else:
            print("Too high. Try again!")

if __name__ == "__main__":
    guess_the_number()

Related Projects




Related Projects


Recent Comments

Latest Comments section by users