Here’s a simple text-based game written in Python: ”Guess the Number”

TechVerse Chronicles
2 min readSep 18, 2024

--

### Game: **Guess the Number**

In this game, the computer randomly selects a number, and the player has to guess it.

```python
import random

def guess_the_number():
# The computer selects a random number between 1 and 100
secret_number = random.randint(1, 100)
attempts = 0

print(“Welcome to ‘Guess the Number’!”)
print(“I have selected a number between 1 and 100.”)
print(“Can you guess what it is?”)

while True:
# Ask the user to make a guess
guess = int(input(“Enter your guess: “))
attempts += 1

# Check if the guess is correct
if guess < secret_number:
print(“Too low! Try again.”)
elif guess > secret_number:
print(“Too high! Try again.”)
else:
print(f”Congratulations! You guessed the number {secret_number} in {attempts} attempts.”)
break

# Start the game
guess_the_number()
```

### How the Game Works:
1. The program randomly selects a number between 1 and 100.
2. The player tries to guess the number.
3. The program provides feedback:
— If the guess is too low, it tells the player to guess higher.
— If the guess is too high, it tells the player to guess lower.
4. When the player guesses the correct number, the game ends and displays the number of attempts.

### Example Output:
```
Welcome to ‘Guess the Number’!
I have selected a number between 1 and 100.
Can you guess what it is?
Enter your guess: 50
Too low! Try again.
Enter your guess: 75
Too high! Try again.
Enter your guess: 60
Congratulations! You guessed the number 60 in 3 attempts.
```

You can run this game in any Python environment or online Python editor. It’s a fun and simple way to introduce logic, loops, and conditionals in Python!

--

--

TechVerse Chronicles
TechVerse Chronicles

Written by TechVerse Chronicles

Python learner, experienced banker, dedicated father, and loving son, on a journey of growth and discovery. Passionate about coding and family life.

No responses yet