CodingMSTR LogoCodingMSTR
Guess The Number Game in Python Source Code

Guess The Number Game in Python Source Code

Free

<b>Guess the number game in Python Source Code</b>

Category: PythonGames
Added On: November 23, 2023
Developer: By Praveen

For any customization or code setup, feel free to contact us. We also offer deployment on live servers.

For any issues related to downloading, email me at devpraveenkr@gmail.com

Need additional support or customization? Contact me!

Project Description

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()