Random Number Guessing Game In Python.

The number-guessing game is a beginner project, it is a simple and easy game where the player has to guess a number within a given range. The game is all about fun to test the skills and intuition of a player. The game starts by generating a random number within a specified range, often between 1 and 100. The main objective of this game is to guess the correct number within a few attempts.

At the beginning of the game, the player is presented with a prompt to enter their guess. The player inputs the number that he guesses. The program then compares their guess with the generated number and provides the output screen.


If the guess is higher than the generated number, the program displays that their guess is higher than the generated number. Similarly, if the guess is lower, the program lets the player know that their guess is too low. This process continues until the player gets it correct.


Once the player successfully guesses the number, the game congratulates them and displays the correct number. Additionally, it provides the player with information about the number of attempts it took to guess correctly. The player can then choose to play again or exit the game.


Flow chart:


1. Start

2. Generate a random number between 1 and 100

3. Set count = 0

4. Display the welcome message and game objective

5. Start loop

    6. Prompt the player to enter their guess

    7. Increment count by 1

    8. Compare the guess with the generated number

    9. If guess < number, display "Too low! Try again."

    10. If guess > number, display "Too high! Try again."

    11. If guess == number, display "Congratulations! You guessed the number correctly!"

        12. Display the generated number

        13. Display count

        14. End loop

15. End


The flow describes all the steps that are taken to build a guessing game here I will explain all the steps.

First of all the computer selects a random number in the range 1 to 100. The number generated by the program is kept secret and you have to guess that number isn't it interesting.


Before starting the game the number of counts(guesses) is set to zero.


At the starting point of the game, a message welcomes you to the Game. The Program will inform you that a random number is picked by the system and you have to guess it.


Now it's time to make your first guess! You enter a number of your choice.


The program compares your choice with the randomly generated number. If the number is greater it will print a message too high. If the guess is lower it will print too low. if it matches it prints congratulations.


You continue making guesses, and the program keeps providing feedback based on whether your guesses are too low or too high. The number of attempts you've made is also counted and incremented with each guess.


Eventually, you'll make a guess that matches the secret number. Congratulations! The program will display a message telling you that you've successfully guessed the number and also let you know the number of attempts it took you to get it right.


The game ends, and you can choose to play again if you'd like.


Here is the complete code:


import random


def guess_number():

    number = random.randint(1, 100)

    count = 0


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

    print("I'm thinking of a number between 1 and 100. Can you guess it?")


    while True:

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

        count += 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()


Explanation:


Import a random module. 

define a function using the def keyword.

initialize the guess counter to zero.

print the welcome message, and instructions for the game.

start the game loop.

take the input from the player.

increment the counter.

check the condition by comparing the input with the random number.

print the result based on the comparison.

invoke the guess function.



Thanks, Happy Coding!


Comments

Popular posts from this blog

Python Functions: A Comprehensive Guide with Examples

Python tuples

Python program to Add two numbers