
๐ 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