Day 8: A Number-Guessing Game In Python

Welcome to Day Eight of my 21-day project series!

Today, I have made a pretty simple and fun Number-Guessing Game In Python. It is not a GUI, not an AI. Just a simple guessing game.

But trust me or not this simple game is super exciting to play. Especially when you are bored. You can play it here if want to. (It’s quite slow and laggy you can go on to play this one too…)

Project Contents

This mini-project is a simple number-guessing game.

The program will generate a random number between 1 and 50.

The player is then prompted to guess the number within a limited number of attempts (10 in this case).

After each guess, feedback is provided to help the player narrow down their choices. The game ends when the player either guesses the correct number or exhausts all the attempts.

I have used three built-in modules random, time, and os module.

Overall, it will be a fun game to play…

Full Source Code

Here’s the full source code of the Number Guessing Game with Python.

import random
import time
from os import system

MAX_ATTEMPTS = 10
MIN_NUMBER = 1
MAX_NUMBER = 50


def introduce() -> None:
    """Introduces player to the rules of the game."""
    print("You will get a total of 10 guesses.")
    time.sleep(2)
    print("In 10 guesses you have to guess the correct no. between 1 to 50.")
    time.sleep(2)
    print("READY???")
    for count in range(1, 4):
        print(count)
        time.sleep(1)
    print("GO")
    time.sleep(1)
    system("cls")

def play_number_guessing_game() -> None:
    """
    Plays the number guessing game.
    
    A random number is generated between MIN_NUMBER and MAX_NUMBER.

    The player is prompted to guess the number within a limited number of attempts.
    Feedback is provided after each guess.
    
    The game ends when the player guesses the correct number or exhausts all attempts.
    """
    num = random.randint(MIN_NUMBER, MAX_NUMBER)
    guess_list = []

    introduce()
    attempt = 1
    while attempt != (MAX_ATTEMPTS + 1):
        try:
            guess = int(input("Guess a no.:"))

        except ValueError:
            print("Enter a valid number!!")
            continue

        if guess == num:
            print(f"\n\nYou got it right in {attempt} attempts.\nThe no. is {num}")
            return
        elif guess in guess_list:
            print("You already guessed this no. before!")
        elif guess > num:
            print(f"The no. is less than {guess}")
            attempt += 1
            print(11-attempt,"Attempts left")
        elif guess < num:
            print(f"The no. is greater than {guess}")
            attempt += 1
            print(11-attempt,"Attempts left")
        guess_list.append(guess)

    if attempt>=10:
        system("cls")
        print("GAME OVER")
        print("The correct no. was",num)

if __name__ == "__main__":
    play_number_guessing_game()

Possible Improvements (That I’ll add after 21 days)

So, here are the possible improvements in the Number-Guessing Game In Python:

  1. Difficulty levels
  2. Score tracking
  3. Hints
  4. Time limit
  5. Power-ups
  6. Multiplayer mode
  7. Sound effects and visuals
  8. Customizable settings

And a lot more…

Experience of the day

In case you don’t know this project is a part of the brand new series that I have started. #21days21projects in python. To read more about it you can check out this page.

And for those who already know here’s my experience of the day.

Today, I wanted to make something fun and light. Cause it’s the weekend and I am in a full chilled-out mood. Also, I had a time constraint due to some prior commitments to friends.

So, I had to make something that was fun, easy, and fast. First of I thought of making a word-guessing game. As I already made it once beforehand.

But guess what I couldn’t find it all over the computer. That’s where I learned a lesson.

I should have the code I write online. Even if it’s not on something like Github I should at least have it somewhere.

This was a simple project. but what if I had a whole website or a machine-learning model? It would be gone. All my previous efforts will be a waste.

This also helped me see the progress I have made in these years. I got to read some code I wrote earlier. As I dumped it on an online note-keeping web app.

So, a suggestion, go and just simply copy-paste all the code you have to some online storage. You don’t need to keep it there in a beautiful manner just copy and then paste. You will bless me for this later…

Do let me know whether you liked this Mini Calculator GUI with Python or not. Also, let me know if there are any suggestions.

And most importantly how was your DAY 8?

Chilled like mine?

Well, I hope it was fun. And remember it’s perfectly awesome if you missed out. Continue to work from tomorrow.

And today just write one line of code as simple as making a variable or printing something.

Your suggestions are welcomed!

Happy coding…

And wait, if you have some ideas for the upcoming projects do let me know that too. Even when the series ends do let me know what you like…

Leave a Reply