Number Guessing Game

How to Create A Number Guessing Game in python | python project

How to Create A Number Guessing Game in python

Hello coder, welcome to the codewithrandom blog. In this article, we will Create A Number Guessing Game using Python with Complete Source Code.

What is Number Guessing Game?

A number guessing game is a game where the user is asked to guess a randomly generated number within a certain range. Typically, the user is given hints, such as whether their guess is too high or too low, and they continue to guess until they correctly identify the secret number. Number guessing games can be a fun way to pass the time and can also help develop critical thinking and problem-solving skills.

 Number Guessing Game

How To Run The Code :

step 1: open any python code Editor.

step 2:  Make a python file main.py

step 3:  import random module

step 4:  Copy the code & Past it

step 5:  Run the file main.py and your program will run

 

Complete Code( with proper comments ) 👇👇

import random

print("Number guessing game")

# randint function to generate the
# random number b/w 1 to 9
number = random.randint(1, 9)

# number of chances to be given
# to the user to guess the number
# or it is the inputs given by user
# into input box here number of
# chances are 5
chances = 0

print("Guess a number (between 1 and 9):")

# While loop to count the number
# of chances
while True:

    # Enter a number between 1 to 9
    guess = int(input())

    # Compare the user entered number
    # with the number to be guessed
    if guess == number:

        # if number entered by user
        # is same as the generated
        # number by randint function then
        # break from loop using loop
        # control statement "break"
        print(
            f'CONGRATULATIONS! YOU HAVE GUESSED THE \
            NUMBER {number} IN {chances} ATTEMPTS!')
        # Printing final statement using the f-strings method;
        break

    # Check if the user entered
    # number is smaller than
    # the generated number
    elif guess < number:
        print("Your guess was too low: Guess a number higher than", guess)

    # The user entered number is
    # greater than the generated
    # number
    else:
        print("Your guess was too high: Guess a number lower than", guess)

    # Increase the value of chance by 1
    chances += 1

Output👇👇

Number Guessing Game

Conclusion

And there you have it, a simple number guessing game in Python! Hope you enjoyed building with us! Visit our homepage and you get lot’s of projects💝.

Build A Flames Game in Python | Python Project

Build a Tic-Tac-Toe Game Using Python

Make Your Own Brick Breaker Game Using Python

Make a rock, paper, scissors game in Python

 



Leave a Reply