Friday, 28 August 2026

๐Ÿš€ DAY 105/150 – Number guessing game

 



๐Ÿš€ Day 105/150 – Number Guessing Game in Python


A Number Guessing Game is a simple and fun Python project that helps beginners understand **random numbers, user input, conditional statements, loops, and comparison operators**.

In this post, we'll explore three different ways to build a Number Guessing Game in Python — from a basic version to a version with hints and limited attempts.


Method 1 – Basic Guess


The simplest version generates a random number between 1 and 10 and asks the user to guess it.


import random n = random.randint(1, 10) g = int(input("Guess: ")) print("Correct!" if g == n else "Wrong!")


Sample Output

Guess: 7
Correct!

Explanation

`import random` imports Python's random module.

`random.randint(1, 10)` generates a random integer between 1 and 10.

`input()` takes the user's guess.

`int()` converts the entered value into an integer.

The conditional expression checks whether the guess is equal to the generated number.

If both numbers match, `"Correct!"` is displayed; otherwise, `"Wrong!"` is displayed.

This is the easiest version and is perfect for understanding the basic concept.


Method 2 – Guess With Hint


We can make the game more interactive by telling the player whether their guess is too high or too low.

import random n = random.randint(1, 10) g = int(input("Guess: ")) if g < n: print("Too Low!") elif g > n: print("Too High!") else: print("Correct!")











Sample Output


Guess: 4
Too Low!


Explanation

The program first generates a random number between 1 and 10.

The player enters a guess.

If the guess is smaller than the secret number, `"Too Low!"` is displayed.

If the guess is greater than the secret number, `"Too High!"` is displayed.

If neither condition is true, the guess must be correct, so `"Correct!"` is displayed.

This version introduces **if, elif, and else**, making the game more interactive.


Method 3 – Number Guessing Game With 3 Attempts

We can make the game more challenging by giving the player only three attempts.

import random n = random.randint(1, 10) for i in range(3): g = int(input("Guess: ")) if g == n: print("๐ŸŽ‰ Correct!") break else: print("❌ Game Over!")











Sample Output

Guess: 3
Guess: 8
Guess: 6
๐ŸŽ‰ Correct!


Explanation

`random.randint(1, 10)` generates the secret number.

`for i in range(3)` allows the player to make a maximum of three guesses.

Inside the loop, the user enters a guess.

If the guess matches the secret number, `"๐ŸŽ‰ Correct!"` is displayed.

The `break` statement immediately stops the loop when the correct answer is found.

The `else` block belongs to the `for` loop. It executes only when the loop finishes all three attempts without encountering `break`.

If the player fails all three attempts, `"❌ Game Over!"` is displayed.

This version introduces an important Python concept: **`for...else`**.

---

Comparison of Methods


| Method                  | Best For                               |
| ---------------          | -------------------------------------- |
| Basic Guess           | Understanding random numbers and input |
| Guess With Hint     | Learning conditional statements        |
| 3 Attempts              | Practicing loops and `break`           |

๐Ÿ”ฅ Key Takeaways


* `random.randint()` can be used to generate a random number.
* `input()` allows the player to enter a guess.
* `if`, `elif`, and `else` help compare the user's guess with the secret number.
* `for` loops can be used to provide multiple attempts.
* `break` stops the game when the correct number is guessed.
* Python's `for...else` can detect when all attempts are completed without success.
* Number Guessing Game is a great beginner project for practicing Python fundamentals.

๐Ÿš€ **Stay tuned for Day 106 of the #150DaysOfPython series!**







0 Comments:

Post a Comment

Popular Posts

Categories

100 Python Programs for Beginner (119) AI (340) Android (25) AngularJS (1) Api (7) Assembly Language (2) aws (31) Azure (12) BI (10) book (1) Books (347) Bootcamp (14) C (78) C# (12) C++ (83) cloud (1) Course (90) Coursera (303) Cybersecurity (36) data (10) Data Analysis (46) Data Analytics (31) data management (16) Data Science (423) Data Strucures (18) Deep Learning (217) Django (16) Downloads (3) edx (21) Engineering (15) Euron (30) Events (7) Excel (24) Finance (13) flask (4) flutter (1) FPL (17) Generative AI (77) Git (13) Google (54) Hadoop (3) HTML Quiz (1) HTML&CSS (48) IBM (43) IoT (3) IS (25) Java (99) Leet Code (4) Machine Learning (394) Meta (24) MICHIGAN (5) microsoft (13) Nvidia (8) Pandas (16) PHP (20) Projects (34) Python (1363) Python Coding Challenge (1228) Python Library (1) Python Mathematics (14) Python Mistakes (51) Python Quiz (614) Python Tips (105) Questions (3) R (72) React (7) Scripting (3) security (4) Selenium Webdriver (4) Software (21) SQL (55) Udemy (20) UX Research (1) web application (11) Web development (9) web scraping (3)

Followers

Python Coding for Kids ( Free Demo for Everyone)